add a key-value pair to a map in ruby

To add a key-value pair to a map in Ruby, you can use the [] method or store method. Here's an example:

main.rb
my_map = { "apple" => 2, "banana" => 3 }
my_map["orange"] = 4
my_map.store("pear", 1)

puts my_map # => {"apple"=>2, "banana"=>3, "orange"=>4, "pear"=>1}
154 chars
6 lines

In the example above, we have a my_map hash that contains a few key-value pairs. To add a new key-value pair, we can use either the [] method or store method, followed by the key and value we want to add.

After adding the new key-value pairs, we print the updated my_map hash using puts. The output shows that the new key-value pairs have been successfully added to the hash.

gistlibby LogSnag