Updated April 3, 2023
Definition of Ruby Hashes
Hash in the ruby is a way of storing data like an array, the only difference is it stores data in the form of key-value pairs, not in the form of an index like {“name”=>”ranjan”}. We can access the value at any position without knowing the index value and hence no need to traverse the whole hash to get the value of any key, simply by name of the key we can get the value. In case the value is not there for the searched key it will return nil, it’s very useful to store data in the hash as at the time of accessing the data even data is not there for the particular key it will return default.
Syntax:
In the below syntax we can see the hash with key and value, if we want to access the value3 then by H[“key3”] we can get the value.
H={"key1"=>"value1", "key2"=>"value2","key3"=>"value3"}
How to Create Hashes in Ruby?
There are various ways to create the hash in the Ruby, be are some of the important ways,
1. Using the New Class
We can simply pass the string as an initial value and we can access it with the 0 indexes. In the below example we have created a hash of student and student[0] is giving the value, student[10] is giving the default value here.
Code:
student = Hash.new( "student" )
puts "#{student[0]}"
puts "#{student[10]}"
Output:
2. Using Hash keyword of Ruby
In the below example we are assigning the value for the a, b, and c key and with the help of the same key, we are able to access each attribute. Please see the below example with the output screen.
Code:
students = Hash["a" => "ranjan", "b" => "akash","c"=>"vikash"]
puts "#{students['a']}"
puts "#{students['b']}"
puts "#{students['c']}"
Output:
How to Modify Hashes in Ruby?
To modify hashes in ruby we can use each or any other looping function of the ruby. In the below example we have defined an array of students with a small case and then we are changing them to upper case with the help of upcase function of a ruby.
Code:
students ={"a" => "ranjan", "b" => "ajay", "c" =>"akash"}
puts students
students.each { |key, value| students[key] = value.upcase }
puts students
Output:
Hash Methods in Ruby
- new: We can create an empty hash with the help of a new class of ruby.
- Try_convert: This method will be used to convert objects into the hash value. In case if it will not be able to convert it will return nil.
- <: It will check if the one hash is the subset of the other hash or not, it will return true if the hash is a subset of the other hash.
- <=: It will check if the given hash is equal or a subset of the other hash. It returns true if the one hash is the subset or equal to the other hash.
- ==: It checks for the equal condition for the one hash from the other hash. It will return true if one hash is equal to another hash.
- clear: In case if we wanted to make empty to all the key-value pairs of the hash then we can use this attribute.
- delete: In case if you wanted to remove one particular key-value pair from the hash then we can use this attribute. In case if the key-value pair which we are trying to delete is not available it will return nil.
- Delete_if: This we use for the conditional delete. Here, in this case, it will delete every key-value pair from the hash for the block logic which we will write.
- each: It will allow us to traverse through the all key-value pair for the given hash.
- empty: It will check for the data into the hash, it will return true in case if there is no data in the hash.
- fetch: This method will be used to fetch the value for the given key. In case if the key for which we run the fetch method is not there it will return a default value if set.
- has_key: This method will be used to check if the key is present there, it will return true in case if the key is there into the hash.
- has_value: It will check for the value for any given key, it returns true if the value is there for the given key.
- to_s: It will be used to convert the hash into the string.
- invert: This method is used to create a new hash with exchange the key-value pair, which means the key will be replaced with the value and the value will be replaced with the key. Suppose in case if a key with the same which going to created then, in that case, the last one will be key-value pair will be considered.
- length: This method is used to get the number of key-value pairs inside the hash.
Example of Ruby Hashes
In the below example we are using a caparison operator for comparing the hash value. Here hash1 and contains one common key-value but it is not equal to hash2 where hash2 and hash3 contain all the common key-value pairs, so they are equal. In screen output, we can see the boolean output of true and false.
Code:
hash1 = { "aa" => 11, "c" => 2 }
hash2 = {9 => 90, "h" => 10, "aa" => 11 }
hash3 = { "aa" => 11, "h" => 10, 9 => 90 }
hash4 = { "c" => 1, "y" => 2, "l" => 35 }
puts hash1 == hash2
puts hash2 == hash3
puts hash3 == hash4
Output:
Advantages of Hashes in Ruby
There are many advantages of using the Hashes in ruby some of them are given below.
- Because of the key-value pairs, they are always unique as no duplicate key will be there.
- To access any value we can do it directly with the key instead of looping on the whole hash.
- Hashes class has a lot of methods which I have already mentioned above.
- We can use any object for the index (like in an array we can only use the integer index).
- It contains the default value if the searched key does not found it will return the set default values, in case the default value is not set it all return nil.
Conclusion
From the above tutorial we learned the important features of the hashes in Ruby and we learned the uses and it’s an important method that can play an important role while doing any difficult manipulations on the hashes, we finally Shaw the important advantages of the hashes in the Ruby.
Recommended Articles
We hope that this EDUCBA information on “Ruby Hashes” was beneficial to you. You can view EDUCBA’s recommended articles for more information.