Ruby Hash shift()方法
Hash#shift()是一个Hash类方法,它从哈希中删除一个键值对,然后将这些值作为双项数组返回。
语法。Hash.shift()
参数:哈希值
返回:双项[键-值]数组
例子 #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"
输出:
Hash a shift form : [:a, 455]
Hash b shift form : [:e, 500]
Hash c shift form : [:a, 100]
例子 #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"
输出:
Hash a shift form : ["a", 344]
Hash b shift form : ["a", 100]
Hash c shift form : ["e", 100]