Hash#shift() is a Hash class method which removes a key-value pair from hash and then it returns these value as two-item array.
Syntax: Hash.shift()
Parameter: Hash values
Return: two-item [key-value] array
Example #1 :
# Ruby code for Hash.shift() method # declaring Hash value a = {a:455, b:200} # declaring Hash value b = {e:500, c:300, b:200} # declaring Hash value c = {a:100} # shift Value puts "Hash a shift form : #{a.shift()}\n\n" puts "Hash b shift form : #{b.shift()}\n\n" puts "Hash c shift form : #{c.shift()}\n\n" |
chevron_right
filter_none
Output :
Hash a shift form : [:a, 455] Hash b shift form : [:e, 500] Hash c shift form : [:a, 100]
Example #2 :
# Ruby code for Hash.shift() method # declaring Hash value a = { "a" => 344, "b" => 200 } # declaring Hash value b = {"a" => 100} # declaring Hash value c = {"e" => 100, "c" => 300, "b" => 200} # shift Value puts "Hash a shift form : #{a.shift()}\n\n" puts "Hash b shift form : #{b.shift()}\n\n" puts "Hash c shift form : #{c.shift()}\n\n" |
chevron_right
filter_none
Output :
Hash a shift form : ["a", 344] Hash b shift form : ["a", 100] Hash c shift form : ["e", 100]
Recommended Posts:
- Ruby | Hash value? method
- Ruby | Hash < method
- Ruby | Hash > method
- Ruby | Hash select() method
- Ruby | Hash replace() method
- Ruby | Hash to_s method
- Ruby | Hash to_a method
- Ruby | Hash size() method
- Ruby | Hash store() method
- Ruby | Hash values_at method
- Ruby | Hash to_hash method
- Ruby | Hash to_h method
- Ruby | String hash method
- Ruby | Hash to_proc method
- Ruby | Hash class >= method
- Ruby | Hash class <= method
- Ruby | Hash reject method
- Ruby | Hash reject! method
- Ruby | Hash length() method
- Ruby | Hash select!() method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

