Variables and Data Types in Ruby

In this lesson, we are about to see different types of variables and constants. Although there are several types of variables like class variables and global variable, we will learn about local variables in this lesson. To create a variable in ruby, we simply provide a name and value using the assignment statement.

number = 100

Rules in naming variables

  1. Must begin with lowercase letters or underscore like : number, _name. Underscore come in handy when we create a compound variable using Snake Case like,
    first_name = "Bharath"
    Another way to write compound variable or multiword variable is Camel Case.
    lastName = "Kumar"

    The Ruby Style Guide recommends consistent conventions and best practices to Ruby developers.

  2. Followed by the underscore or lowercase letter the next character of a variable can be a number or other letters, but not symbols.
    _123, rate1  //valid 
    #abc, n*ame  //invalid variable names.
    
  3. Names should match any of the reserved keywords. We'll look at the reserved keywords in ruby in the reserved keywords lesson.

  4. Global variables are created by putting a dollar symbol $ before the variable name. Ex. $salary = 10000. salary is a global variable which can be seen throughout the program.


Number Data Type

In this section, we are going to work with numbers data type. Ruby supports major number systems such as whole numbers, floating point numbers. Different bases such as Binary, Octal and Hexadecimal are allowed.

Binary, Hexadecimal, octal representation in ruby

To write in binary we use 0b and the corresponding number. For example, 5 can be written as 101 in binary. Likewise, to represent number in hexadecimal 0x is used and for octal 0 alone is used. Negative numbers are represented by using unary operator - (minus) before the number.

Ruby supports different arithmetic operations such as addition, subtraction, multiplication, division, modulus and exponentiation.

arithmatic operations in Ruby

In above image, you can see the arithmetic operations performed.

  • 9 % 3 statement provides the remainder when 9 is divided by 3. In this case the result will be 0.
  • 2 ** 5 statement raises 2 to the power of 5. So, the result will be 2 * 2 * 2 * 2 * 2, which is equal to 32.

We will learn about operators in detail in a subsequent lesson.


ABS Method:

abs is a method used to find the absolute value of a number. For example, -26.abs gives the value of 26. Some other methods are div and modulo.

div and modulo methods in Ruby


You can also convert numbers to strings. For example, to convert 100 into a string :

100.to_s

integer to string conversion in Ruby


Boolean data type

Boolean type variables can hold two values true or false. But in ruby, it can also hold third type of value in Boolean variable called nil. But in most context, we work with only true and false values. So, let’s concentrate on those two.

Boolean data type is used in comparison, loops and decision making statements. 100 > 1 returns true. Because 100 is greater than one. Likewise,1 > 100 returns false.

You can also assign Boolean values to the variables.

flag = true

After this assignment, the variable flag contains the Boolean value true.


boolean datatype in Ruby

Not operator (!) negates the Boolean value. For example, if the flag variable contains the value true. Then the negation of the flag variable gives the value of false.

double negation in Ruby

You can also double negate the variable. But negating more than once tends to be confusing. Hence it’s best to stick with single negate when you need it.


Constants

Constants are variables that holds the same value throughout the program. Ruby is a typical language, where you can reassign constants. As per conventions, all constants in ruby in uppercase characters so that they are easily distinguishable.

Constants in Ruby

Notice that it did change the value of the constant, but it gave a warning saying that "PI is already initialized constant".

So, just be aware that you can define constants in ruby, you can also change its value which means you wonder they shouldn't be called constants. As we already said, Ruby is a typical language.