Ruby memorandum (acquisition of key value)

Ruby memorandum (acquisition of key value)

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


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 ().


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

Recommended Posts

Ruby memorandum (acquisition of key value)
Acquisition of article information in ruby ​​scraping
[Ruby] Method memorandum
Basics of Ruby
[Ruby] I want to extract only the value of the hash and only the key
[Ruby on Rails] A memorandum of layout templates
definition of ruby method
[ruby] How to assign a value to a hash by referring to the value and key of another hash
Minimum value (ruby edition)
ruby memorandum (basic edition)
Setting value of
Determine that the value is a multiple of 〇 in Ruby
Basic methods of Ruby hashes
Image acquisition memorandum gallery call
Basic methods of Ruby arrays
[Ruby] Various types of each
[Ruby] List of basic commands
Memorandum of understanding about LOD.
Judgment of fractions in Ruby
Ruby on Rails Basic Memorandum
Review of Ruby basic grammar
I want to change the value of Attribute in Selenium of Ruby