Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add to an existing hash in Ruby

In regards to adding an key => value pair to an existing populated hash in Ruby, I'm in the process of working through Apress' Beginning Ruby and have just finished the hashes chapter.

I am trying to find the simplest way to achieve the same results with hashes as this does with arrays:

x = [1, 2, 3, 4] x << 5 p x 
like image 577
Tom Avatar asked Jul 28 '11 18:07

Tom


People also ask

How do you add something to a hash in Ruby?

Hash literals use the curly braces instead of square brackets and the key value pairs are joined by =>. For example, a hash with a single key/value pair of Bob/84 would look like this: { "Bob" => 84 }. Additional key/value pairs can be added to the hash literal by separating them with commas.

How do you add hash to hash?

Merge two hashes On of the ways is merging the two hashes. In the new hash we will have all the key-value pairs of both of the original hashes. If the same key appears in both hashes, then the latter will overwrite the former, meaning that the value of the former will disappear. (See the key "Foo" in our example.)

Can you push to a hash Ruby?

No, push won't work that way (as the error tells you). push is only defined for Arrays, but in your Hash, you don't have Arrays. You have Symbols ( :lotr , :batman ) as keys, and numbers as values. Neither Symbols nor numbers have a push method, which is why you are getting the error.

How do you combine hashes in Ruby?

We can merge two hashes using the merge() method. When using the merge() method: Each new entry is added to the end. Each duplicate-key entry's value overwrites the previous value.


2 Answers

If you have a hash, you can add items to it by referencing them by key:

hash = { } hash[:a] = 'a' hash[:a] # => 'a' 

Here, like [ ] creates an empty array, { } will create a empty hash.

Arrays have zero or more elements in a specific order, where elements may be duplicated. Hashes have zero or more elements organized by key, where keys may not be duplicated but the values stored in those positions can be.

Hashes in Ruby are very flexible and can have keys of nearly any type you can throw at it. This makes it different from the dictionary structures you find in other languages.

It's important to keep in mind that the specific nature of a key of a hash often matters:

hash = { :a => 'a' }  # Fetch with Symbol :a finds the right value hash[:a] # => 'a'  # Fetch with the String 'a' finds nothing hash['a'] # => nil  # Assignment with the key :b adds a new entry hash[:b] = 'Bee'  # This is then available immediately hash[:b] # => "Bee"  # The hash now contains both keys hash # => { :a => 'a', :b => 'Bee' } 

Ruby on Rails confuses this somewhat by providing HashWithIndifferentAccess where it will convert freely between Symbol and String methods of addressing.

You can also index on nearly anything, including classes, numbers, or other Hashes.

hash = { Object => true, Hash => false }  hash[Object] # => true  hash[Hash] # => false  hash[Array] # => nil 

Hashes can be converted to Arrays and vice-versa:

# Like many things, Hash supports .to_a { :a => 'a' }.to_a # => [[:a, "a"]]  # Hash also has a handy Hash[] method to create new hashes from arrays Hash[[[:a, "a"]]] # => {:a=>"a"}  

When it comes to "inserting" things into a Hash you may do it one at a time, or use the merge method to combine hashes:

{ :a => 'a' }.merge(:b => 'b') # {:a=>'a',:b=>'b'} 

Note that this does not alter the original hash, but instead returns a new one. If you want to combine one hash into another, you can use the merge! method:

hash = { :a => 'a' }  # Returns the result of hash combined with a new hash, but does not alter # the original hash. hash.merge(:b => 'b') # => {:a=>'a',:b=>'b'}  # Nothing has been altered in the original hash # => {:a=>'a'}  # Combine the two hashes and store the result in the original hash.merge!(:b => 'b') # => {:a=>'a',:b=>'b'}  # Hash has now been altered hash # => {:a=>'a',:b=>'b'} 

Like many methods on String and Array, the ! indicates that it is an in-place operation.

like image 124
tadman Avatar answered Oct 06 '22 01:10

tadman


my_hash = {:a => 5} my_hash[:key] = "value" 
like image 33
robbrit Avatar answered Oct 06 '22 00:10

robbrit