Variables in Ruby

Variable:

Now we want to store our data and want to use that data again and again. We store our data in memory. This memory is assign to a variable.

  1. #we are defining a variable
  2. var = 'harish'
  3. puts var
  4. # we are modifying that value
  5. var = 'another value'
  6. puts var

Here 'var' is a variable which stores the value 'this is a variable' in memory. This types of variables are called local variables. There are four more types of variables: instance variables, class variables, global variables and constant variables. We are not going to read about every variable at this time because I am assuming my readers are new with programming. We will read them when we will have need of these variables. At this time we discuss about constants.

Constants:

As clear from name, these are such variables whose values are fixed. We can define them only once and after that we can not modify them. Why do we need these variables? Think about constants like PI and other constants in formulas.

  1. # Now we are defining a constant
  2. # First letter of constant is always capital
  3. PI = 3.14
  4. puts PI

Note that first letter of the constants are always capital. It is good practice to write all letters of a constant capital.

Printing variables in string:

We can print variables inside the string:

  1. var = 'this is a variable'
  2. VAR = 'this is a constant'
  3. puts "now we are printing: #{var}"
  4. puts "now we are printing: #{VAR}"

We will get output:

  1. now we are printing: this is a variable
  2. now we are printing: this is a constant

Reserved keywords in Ruby:

Just like every language ruby language has also some reserved keywords which we can not use for giving name to any variable or constant.

BEGINdonextthen
ENDelseniltrue
aliaselsifnotundef
andendorunless
beginensureredountil
breakfalserescuewhen
caseforretrywhile
classifreturnwhile
definself__FILE__
defined?modulesuper__LINE__



ReactJS with Redux Online Training by Edureka

About Harish Kumar

Harish, a fullstack developer at www.lyflink.com with five year experience in full stack web and mobile development, spends most of his time on coding, reading, analysing and curiously following businesses environments. He is a non-graduate alumni from IIT Roorkee, Computer Science and frequently writes on both technical and non-technical topics.

Related Articles

PHP stands for PHP: Hypertext Preprocessor. PHP is known as a server side scripting and interpreter language for web ...
Introduction to PHP
In this tutorial, we will study about conditional statements (if else statements and switch statements). These statem...
Conditional statements in php
In this tutorial, we will study about strings and operations upon strings. Due to long length of this section, I have...
Strings in PHP (part-1)

Complete Python Bootcamp: Go from zero to hero in Python 3

Top Posts

Recent Posts

The Complete Web Developer Course - Build 25 Websites

Subscribe

Subscribe now and get weekly updates directly in your inbox!