[RUBY] About hashes

Article on the previous symbol

Use symbols for hash keys

As I mentioned in my last article on symbols, symbols are a better choice for hash keys than strings. If you use a symbol for the hash key, you get the following code.

menu = { :food => 'rice', :dessert => 'cake'}
#When retrieving a value
menu[:dessert] # => "cake"
#When adding a new key / value combination
menu[:drink] = 'water'
p menu # => {:food=>"rice", :dessert=>"cake", :drink=>"water"}

If the symbol is the key, you can create the hash with the notation symbol: value without using =>. (* The position of the colon changes from left to right)


menu = {food: 'rice', dessert: 'cake'}
#Same when fetching values
menu[:dessert] # => "cake"

(Example) Code when both key and value are symbols

{food: :rice, dessert: :cake, drink: :water}

Method keyword arguments and hashes

When passing arguments to a method, use the method's keyword arguments to improve readability. The keyword arguments for the method are defined as follows:

def method name(Keyword argument 1:Default value 1,Keyword argument 2:Default value 2)
#Method implementation

(Example using keyword arguments)

def buy_food(menu, drink: true, dessert: true)
  #Buy food
  if drink
    #Buy a drink
  if dessert
    #Buy dessert
#Buy rice, drinks and desserts
buy_food('rice', drink: true, dessert: true)
#Buy pizza and drinks
buy_food('pizza', drink: true, dessert: false)

When calling a method with keyword arguments, just as you did when you created the hash Symbol: Specify the argument in the form of a value.

In addition, the keyword argument has a default value, so you can omit the argument.

buy_food('rice', drink: true, dessert: true)

⬇︎ drink and dessert also use the default value of true, so do not specify them.


You can omit the default value of the keyword argument.

def buy_food(menu, drink:, dessert:)

Keyword arguments that do not have default values cannot be omitted during the call. (If omitted, an error will occur.)

buy_food('rice', drink: true, dessert: true)

When calling a method that uses keyword arguments It is also possible to pass a hash (key is a symbol) that matches the keyword argument as an argument.

Params = { drink: true, dessert: false }
buy_food('rice', params)

Referenced literature

Introduction to Ruby for those who want to become a professional

Recommended Posts

About hashes
About Ruby Hashes
About Ruby Hashes (continued)
About =
What you learned about hashes
About Ruby hashes and symbols
About method.invoke
About Kotlin
About attr_accessor
About Hinemos
About inheritance
About params
About Docker
About Rails 6
About form_for
About Spring ③
About enum
About polymorphism
About Optional
About JitPack
About Dockerfile
About this ()
About devise
About encapsulation
About Docker
About active_hash
About static
About exceptions
About scope
[Maven] About Maven
About exception handling
Review about bundler
[Java] About Java 12 features
About Rails routing
About cyclomatic complexity
About exception handling
About AsyncTask Generics
About Ruby symbols
About array multiplication
[Java] About arrays
About HotSpot VM
About ruby ​​form
About Active Storage
About Spring AOP
About singular methods
Ruby Learning # 14 Hashes
About getter setter
About build tools
About MacinCloud introduction
[Note] About nil
About keyword arguments
Chew about API
[About double hash]
About installing Cocoapods
Something about java
Where about java
About HttpServlet () methods
About Java features
About ActiveRecord :: PendingMigrationError
About SIGNING_REGION: NoSuchFieldError