Basic methods of Ruby hashes

I will make a note of the basic methods that can be used with Ruby hashes.

** values_at ** Returns an array of values ​​corresponding to multiple specified keys

menu
=> {"Udon"=>"400 yen", "Soba"=>"350 yen", "ramen"=>"700 yen"}
menu.values_at("Udon","Soba","ramen")
=> ["400 yen", "350 yen", "700 yen"]

Returns nil if there is no corresponding key

menu
=> {"Udon"=>"400 yen", "Soba"=>"350 yen", "ramen"=>"700 yen"}
menu.values_at("Udon","Soba","ramen","Hoto")
=> ["400 yen", "350 yen", "700 yen", nil]

** fetch ** Get the value of the specified key

menu
=> {"Udon"=>"400 yen", "Soba"=>"350 yen", "ramen"=>"700 yen"}
menu.fetch("Udon")
=> "400 yen"
menu.fetch("Soba")
=> "350 yen"

(KeyError) if the key cannot be found

menu
=> {"Udon"=>"400 yen", "Soba"=>"350 yen", "ramen"=>"700 yen"}
irb(main):025:0> menu.fetch("Hoto")
Traceback (most recent call last):
.
.
.
KeyError (key not found: "Hoto")

** invert ** Swap keys and values ​​and return a new hash

menu
=> {"Udon"=>"400 yen", "Soba"=>"350 yen", "ramen"=>"700 yen"}
menu.invert
=> {"400 yen"=>"Udon", "350 yen"=>"Soba", "700 yen"=>"ramen"}

** merge ** merge hashes

menu
=> {"Udon"=>"400 yen", "Soba"=>"350 yen", "ramen"=>"700 yen"}
menu2
=> {"tea"=>"150 yen", "Cola"=>"300 yen"}
menu.merge(menu2)
=> {"Udon"=>"400 yen", "Soba"=>"350 yen", "ramen"=>"700 yen", "tea"=>"150 yen", "Cola"=>"300 yen"}

** store ** Add an element to the hash

menu
=> {"Udon"=>"400 yen", "Soba"=>"350 yen", "ramen"=>"700 yen"}
menu.store("Hoto","600 yen")
=> "600 yen"
menu
=> {"Udon"=>"400 yen", "Soba"=>"350 yen", "ramen"=>"700 yen", "Hoto"=>"600 yen"}

** select ** Pass the key and value in the hash to the block in order, collect the elements for which the block returned true, and return a new hash

test
=> {"Oishi"=>80, "moat"=>45, "Tanaka"=>60, "Takano"=>25}
test.select {|key,value| value >= 50}
=> {"Oishi"=>80, "Tanaka"=>60}

** reject ** Pass the key and value in the hash to the block in order, collect the elements that the block returned false, and return a new hash

test
=> {"Oishi"=>80, "moat"=>45, "Tanaka"=>60, "Takano"=>25}
irb(main):053:0> test.reject {|key,value| value >= 50}
=> {"moat"=>45, "Takano"=>25}

Recommended Posts

Basic methods of Ruby hashes
Basic methods of Ruby arrays
[Ruby] List of basic commands
Ruby methods and classes (basic)
Review of Ruby basic grammar
Basic knowledge of Ruby on Rails
Ruby basic terms
About Ruby Hashes
Ruby Learning # 14 Hashes
Basics of Ruby
Summary of hashes and symbols in Ruby
About Ruby Hashes
Ruby Learning # 15 Methods
About Ruby methods
[Ruby] Basic knowledge of class instance variables, etc.
[Java] Personal summary of classes and methods (basic)
About Ruby Hashes (continued)
Ruby learning points (basic)
ruby basic syntax memo
Ruby Learning # 31 Object Methods
About Ruby instance methods
Basic format of Dockefile
definition of ruby method
Ruby variables and methods
[Ruby] methods, instance methods, etc ...
ruby memorandum (basic edition)
Java engineers now compare to learn the basic grammar of Ruby Part 2 (classes, methods)
Basic knowledge of SQL statements
Review the basic knowledge of ruby that is often forgotten
[Ruby] Various types of each
[Docker] Introduction of basic Docker Instruction
Ruby on Rails basic learning ①
Three Bit Manipulation Methods (Ruby)
About Ruby hashes and symbols
I tried to summarize the basic grammar of Ruby briefly
Super basic usage of Eclipse
What are Ruby class methods?
Judgment of fractions in Ruby
[Ruby] Singular methods and singular classes
[Ruby] Class methods, instance methods, etc.
Summary of basic functions of ImageJ
Ruby variables and functions (methods)
Ruby on Rails Basic Memorandum
[Ruby] Singular methods and singular classes
About the behavior of ruby Hash # ==
Sorting hashes in a Ruby array
Understand the basic mechanism of log4j2.xml
Basics of sending Gmail in Ruby
Various methods of Java String class
Write Ruby methods using C (Part 1)
Implementation of ls command in Ruby
Extraction of "ruby" double hash * Review
[Ruby] See the essence of ArgumentError
Ruby standard input and various methods
Differences in split methods of StringUtils
The basic basis of Swift dialogs
Ruby convenience methods for fledgling engineers
The basic basis of Swift's Delegate
Basic usage of java Optional Part 1
[Ruby] Handle instance variables with instance methods
Various methods of the String class