To get the key or value contained in the hash There is a method prepared in advance.
keys and values methods
Object .keys
Object .values
qiita.rb
puts hash.keys
puts hash.values
This will output all the keys and values contained in the hash.
Given the hash {A: "a"} If you want to retrieve the key A, put the value a paired with A in (). Conversely, if you want to retrieve the value a, put the key A paired with a in ().
qiita.rb
hash = { ringo: "apple", mikan: "orange", ichigo: "Strawberry" }
puts hash.key("apple")
#「ringo: "apple"Is the value of"apple"From the key ringo:Get
puts hash.values_at(:ringo)
# 「ringo: "apple"Is the key":ringo"From the value"apple"Get
Isn't it possible to retrieve by number like an array? to be continued.
Reference site https://www.javadrive.jp/ruby/hash/index8.html https://qiita.com/kidach1/items/651b5b5580be40ad047e
Recommended Posts