Open In App

Ruby | Hash store() method

Last Updated : 13 May, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Hash#store() is a Hash class method that returns an add-on value with the key given by the key-value argument. 

Syntax: Hash.store()
Parameter: Hash values 
key 
value
Return: add on value with the key given by the key-value argument. 

Example #1 : 

Ruby




# Ruby code for Hash.store() method
 
# declaring Hash value
a = {a:100, b:200}
 
# declaring Hash value
b = {a:100, c:300, b:200}
 
# declaring Hash value
c = {a:100}
 
 
# store Value
puts "Hash a store form : #{a.store('e', 67)}\n\n"
 
puts "Hash b store form : #{b.store('d', 467)}\n\n"
 
puts "Hash c store form : #{c.store('g', 647)}\n\n"


Output : 

Hash a store form : 67

Hash b store form : 467

Hash c store form : 647

Example #2 : 

Ruby




# Ruby code for Hash.store() method
 
# declaring Hash value
a = { "a" => 100, "b" => 200 }
 
# declaring Hash value
b = {"a" => 100}
 
# declaring Hash value
c = {"a" => 100, "c" => 300, "b" => 200}
 
# store Value
puts "Hash a store form : #{a.store('e', 67)}\n\n"
 
puts "Hash b store form : #{b.store('d', 467)}\n\n"
 
puts "Hash c store form : #{c.store('g', 647)}\n\n"
 
puts (a)
 
puts (b)
 
puts (c)


Output : 

Hash a store form : 67

Hash b store form : 467

Hash c store form : 647

{"a"=>100, "b"=>200, "e"=>67}
{"a"=>100, "d"=>467}
{"a"=>100, "c"=>300, "b"=>200, "g"=>647}

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads