Updated April 1, 2023
Introduction to Ruby Data Types
Data type is a way to notifying the operating system about the types of data a variable is going to hold through out it’s code journey ,for example if we mention data as integer like a=1 , it means we are notifying system that variable a is going to hold numeric value and in that case system will allocate appropriate space , in the same way if we writing a=true ,it means we are instructing system that a will be boolean only and system will allocate only one bit of space ,in Ruby It supports data types like Numbers ,Boolean ,Strings, Hashes, Arrays, Symbols, we will learn each data types in the example and explanation section.
Types of Ruby Data
As we mentioned, data types are the way to instruct the system about the type of data we are going to assign to any variable or type of variable it will hold throughout it’s journey .Let me explain each with an example .Note that every data type in Ruby is a class since Ruby is a purely object oriented language .
1. Hashes
This is a type of data which holds the data in the form of the key value pair, for example {a=>1,b=>3} . Here main benefits of using hash based data types are, they have key value structure where we can keep any type of data inside a hash variable, which means it allow to hold multiple data types for example a={name=>”ranjan”,age=>30, sex=>”male”}. Let me give you one example for hash data types . We can see the below example , here we have defined a studentDetails hash variable and assign some key value to the student and with the help of each loop we are printing each attribute here.Ruby also provides various methods related to the hash for manipulations and other utility.
Defining the hash containing the details of the students
Code:
studentDetails = { "name" => "ranjan", "age" => 30, "sex" => "male" }
studentDetails.each do |key, attribute|
puts "The student #{key} is #{attribute}"
Output:
2. Arrays
Array is a also a data types which can holds the data of multiple types like hash , but it is different then the hash data types , as in case of hash each element has a key for identifying the value , but in case of array it does not have any key , that means the index for the value of the attribute of array will be created automatically . In Array always the index will be a sequential integer value like 0,1,2,3,4 …. .Let us see one example for the array data type in Ruby. In the below example we have defined a variable with name studentDetails and assigned some array attributes for the students. Here with the help of the function each_with_index function we are iterating over each element of the array and printing the value and index of the data .
Code:
studentDetails = ["ranjan", 30, "male","Chennai" ]
studentDetails.each_with_index do |value, index|
puts "The student #{index} index value is #{value}"
Output:
3. Numbers
In this data type it allows only numeric variables , so if we have defined a variable with numeric value and we are trying to add or any other operation on the variable with string or other non numeric variable it will throw an error , because we have already instructed system about type of variable and the memory allocation for the variable .For example a=1 and b=”x” and if we perform addition of a+b it will not work .Let me show you some examples for it .
In the below i have mentioned three examples for handling the numeric data types.
Here we are adding a numeric variable with the string containing the variable and the output is error , because data types are not the same for both .Please follow the below example along with the screen of output .
Code:
a=1
b="x"
puts a+b
Output:
In this example we are adding two numbers one is integer another one is also integer so the output will come without any external activity .Please follow the below example along with the screen of output .
Code:
a=1
c=2
puts "The result of addition is #{a+c}"
Output:
In this example of the number , we are adding two variables one is with numeric other is with string but we are doing the typecasting of string to the integer with the help of the function to_i.Please follow the below example along with the screen of output .
Code:
a=1
b="2"
puts "The result of addition of a and b is #{a+b.to_i}"
c="x"
puts "The result of addition of a and c is #{a+c.to_i}"
Output:
4. Boolean
In this data type variable we can assign only boolean values like true or false so it takes only one bit of memory space. In the below example we have defined a variable friend and assigned true to it .Please follow the below example along with the screen of output .
Code:
friend =true
if(friend)
puts "We are friends"
else
puts "No , we are not friends"
Output:
5. Strings
As we know strings , it is enclosed with the single(‘’) or double (“”) quotes .Also every string is an object of it’s main class which is a String class .We can see some examples below for the string .
Code:
a="y"
b=2
puts "The result of string concatenation of a and c is #{a+b.to_s}"
c="x"
puts "The result of string concatenation of a and c is #{a+c}"
Output:
6. Symbols
Symbols are very much similar to any string , but in case of symbols its size is very small which means they are lightweight strings . See the below example for it .
Code:
students = {:name => "Ranjan", :address => "Chennai", :Job => "developer"}
puts students[:name]
puts students[:address]
puts students[:Job]
Output:
Conclusion
In this tutorial we learned Data types available in Ruby and understand their practical uses , we learned about the working and various memory and space utilities for different scenarios, we showed some real case examples for all its data types.
Recommended Articles
We hope that this EDUCBA information on “Ruby Data Types” was beneficial to you. You can view EDUCBA’s recommended articles for more information.