RUBY - DATA TYPES
We have seen a variable is used to hold a value. Be it a number or a name or even a sentence.
But how will Ruby come to know, if the value is a number or a name?
Well! Once again, that's the beauty of Ruby. Ruby makes an intelligent guess, if the value is a number or a name or a sentence or something else and create the variable accordingly.
But! Just think for a moment. Ruby must be using some mechanism by which it can determine if the value assigned to a variable is a number or a name or a sentence or something else.
And thus Data Types comes into picture.
Note : In other languages, you have to know all the Data Types to declare a variable. But in Ruby, it is not necessary for you to know the Data Types. You can just assign the values and Ruby will do the task of determining the Data Types.
Data Types
Data Types are the hidden gem that is used by Ruby to determine if the variable is going to hold a number or a name.
But why does Ruby needs to determine if the variable is going to hold a number or a name? Can it not create one type of variable and store all the values there.
It cannot do that. Because if you want to store a number. It would take less space.
On the other hand, if you want to store a String like Hello World. It would take a larger space.
So, whenever you want to create assign a String to a variable.
Ruby makes an intelligent guess and finds out, it is a String type value and creates the storage space accordingly.
The above Data Type for String is called str Data Type.
And similarly for a number, for a decimal value e.t.c. Ruby has different Data Types.
Now, there should be a way for Ruby to differentiate a number from a name.
And thus Data Types comes into picture.
Data Types
Data Types tells the variable that what kind of values it can contain.
Example :
x is a variable, which is holding the value 5.
Below are the set of Data Types supported by Ruby :
Number:
It can hold positive or negative numbers.
Example :
Float:
It can hold positive or negative numbers with decimal points.
Example :
Boolean:
Boolean data type only contains true or false.
Example :
flag1 = true
flag2 = false
String:
It is used to hold a Sentence.
Example :
name = "Surinder Pal Singh Flora"
or
name = 'Surinder Pal Singh Flora'
Symbols:
Symbol is just like a String except it cannot be changed. Just that Strings work with data and Symbols are Identifiers.
Note : We will learn more about Symbols in the coming tutorials.
List of all Data Types used in Ruby
-
String
As we have seen in the above example, a String is a set of letters/alphabets.
String Data Type
The Data Type for String is String itself.
When Ruby finds data inside Double Quotes ("") of Single Quotes (). It creates a String type variable for that value.
Or
So, the Data Type of a variable is determined by Ruby and is hidden from us. Now, if we want to see the Data Type of a variable, Ruby provides a a way that is .class.
Let us look at .class with the below example :
Example :
y = "Hello World"
puts y.class
Output :
String
In the above code we have initialised the variable y with the String Hello World.
Then in the puts statement, we have used y.class Method to get the Data Type of the variable y.
And the y.class had given us the output as String.
-
Numbers
Numbers could be a floating point number (i.e. 5.3322) or a whole number (i.e. 7 or 5).
Let us see them below :
Fixnum Data Type
The Data Type for a whole number is Fixnum.
Example :
Output :
Fixnum
Bignum Data Type
The Data Type for very big numbers is Bignum.
Example :
x = 98765432109876543210
puts x.class
Output :
Bignum
Float Data Type
The Data Type for a floating point number is Float.
Example :
Output :
Float
BigDecimal Data Type
The BigDecimal data type is almost same as Float data type. We use BigDecimal for arbitrary precision arithmetic.
Rational Data Type
The Data Type for a fractional number is Rational. It should have a prefix r while initialising the value.
Example :
Output :
Rational
-
Hash
The Hash, Data Type is almost same as any English Hash.
Just like in an English Hash, you search for any word and find the definition for it.
Similarly, in Hash, Data Type, data is stored in key and value format.
Hash Data Type
Say for example, there are just three students in the class, with roll numbers 1, 2 and 3.
Roll |
Name |
1 |
John |
2 |
Sam |
3 |
Lohit |
Now if the Roll is the key and Name is the value. You can search with the key (i.e. The Roll) and get the value (i.e. The Name).
The declaration of a Hash, Data Type is also quite simple. You can place multiple key and value pairs inside braces {} and Ruby will understand.
Example :
x = {
"1" => "John",
"2" => "Sam",
"3" => "Lohit"
}
puts x
puts x.class
Output :
{"1"=>"John", "2"=>"Sam", "3"=>"Lohit"}
Hash
-
Array
So far we have seen, how to store a Name, a sentence or numbers in separate variables. But what if, we want to store a name and a number in the same variable?
Well! Array is the Solution. Array, is going to store multiple values in one variable.
Array Data Type
The declaration of a list Data Type is super easy. You can place the multiple values inside square brackets [] and Ruby will understand.
Example :
x = [5, "John", "Ruby"]
puts x
puts "The data type is : #{x.class}"
Output :
5
John
Ruby
The data type is : Array
So, if you look at the output. The Data Type of Array is Array.
So, the first line,
Also we can get the values of the Array by using indexes.
Example :
x = [5, "John", "Ruby"]
puts x[0]
puts x[1]
puts x[2]
Output :
5
John
Ruby
So, in the print statement,
Prints the first value of the array that is in index 0(i.e. 5).
-
Booleans
Boolean represents just two values, i.e. true or false.
trueClass and falseClass
Example :
x = true
y = false
puts x
puts "The data type is #{x.class}"
puts y
puts "The data type is #{y.class}"
Output :
true
The data type is trueClass
false
The data type is falseClass