Intro into hashes
Hashes in Ruby
You can throw things in pile and that is fine. Until you need to find something. And then you have to go through every single item to find what you need. But what if there is such a collection where all the data has label on it?
If you have some understanding about arrays, you know that you can find items, process each item in array and that you start at the beginning of the array. Ruby arrays can hold object, including other arrays. Because an array of arrays is not really ideal, we have hashes. Hash is a collection where each value is accessed via a key. Keys are just easier ways to find something — to get back your data. The way I see it is like instead of having a pile of folders where you have to go through all of them to find something, your folders are labeled. Simple as that! 👏
{“M” => “Monday”, “T” => “Tuesday”} #this is hash. "M" is a key,"Monday" is a value, "=>" is key/value separator.
By the way, if you have MAC, you can highlight the text(code), press crtl+alt+6 and post your code snippet . Just throwing that here now .
Back to my hashes. Hashes can grow and they can shrink, as you need. Hashes can hold even other hashes. They can hold instances of more that one class at the same time. Literals of hashes use curly braces but if you want to access individual values, you use square brackets. An array can only use integers as indexes, hash can use any object as a key(numbers, strings, symbols). But if you want to add value, you have to specify each key because keys are not calculated like elements in array.
Hashes are objects
Yes, you know that everything in Ruby is an object. And like objects, they have useful methods.
week_days = {“M” => “Monday”, “T” => “Tuesday, “W” => “Wednesday, “T” => “Thursday, “F” => “Friday” }puts week_days.class # => Hash
puts week_days.length # => 5
puts week_days.keys # => [“Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”]
puts week_days["W"] # => Wednesday
Hashes return “nil” by default
If you try to access a hash key that has never been assigned to, the default value is nil. Ruby treats nil as “falsy”. It is just easier to test whether value has been assigned or not. Lets put it this way, if you access a hash value within an “if” statement, the code inside will be run if value exits.
book= {}if book[“All about arrays in Ruby”] #Value is nil, which is false.
puts “ I will be printed”
end
if book ["All about hashes in Ruby"]=1 #Value is 1, which is truthy.
puts "I will be printed"
end
Some other languages treat empty strings and empty arrays as falsy too but Ruby treats them as truthy. Even the number 0 is truthy in Ruby.
You can use Hash.new to create a new hash. If you pass an object as an argument, that object becomes the hash default object. What that means is when you try to access key that hasn’t been assigned to, the default object will return as the value, instead of nil.
This is only an intro into hashes.They can do so much more.They are powerful and they act like dictionaries. But that can be a subject for next blog maybe 😏
Thanks for reading!
