[Ruby] Arguments with keywords and default values of arguments


version ruby 2.6.3 rails 5.2.3

I was addicted to it because I didn't really understand the arguments with keywords. Even though I used equal to define a method with a default value, I was calling an argument with a keyword when calling the method, so an unintended argument was passed and an error occurred. It was.

This mistake

#Define methods with default valued arguments
def hello(lang = "ja", place = "Japan")
 puts "#{lang} : #{place}"

#How to call arguments with keywords
hello(lang: "en", place: "America")
{:lang=>"en", :place=>"America"} : Japan

The argument was interpreted as a hash and an unintended argument was passed.

About arguments with keywords

Reference https://qiita.com/jnchito/items/74e0930c54df90f9704c

def hello(lang: "ja", place: "Japan")
 puts "#{lang} : #{place}"

#Call with no arguments
ja : Japan

#Call by changing the argument value with keywords
hello(lang: "en", place: "America")
en : America

#Call with non-keyword arguments
hello("en", "America")
wrong number of arguments (given 2, expected 0) 

Calling a method with keyworded arguments without arguments calls the method with default values. An error occurs if you try to call it with a keywordless argument.

About arguments with default values

Reference https://rooter.jp/programming/ruby_method_default/

def hello(lang = "ja", place = "Japan")
 puts "#{lang} : #{place}"

#Call with no arguments
"ja : Japan"

#Call with non-keyword arguments
hello("en", "America")
en : America

Recommended Posts

[Ruby] Arguments with keywords and default values of arguments
Arguments with default values Take the full_title method of the Rails tutorial as an example
[Ruby] Questions and verification about the number of method arguments
Install Ruby 3.0.0 Preview 1 with a combination of Homebrew and rbenv
How to deal with different versions of rbenv and Ruby
Default values for MaxHeapSize and InitialHeapSize
Java arguments, return values and overloads
Write keys and values in Ruby
[Ruby] Boolean values ​​and logical operators
Impressions of making BlackJack-cli with Ruby
Default implementation of Object.equals () and Object.hashCode ()
With ruby ● × Game and Othello (basic review)
Make a note of Ruby keyword arguments
Overload method with Int and Integer arguments
Explanation of Ruby Time and Date objects
Summary of hashes and symbols in Ruby
Extract a part of a string with Ruby
[Ruby] Classification and usage of loops in Ruby
Comparison of JavaScript objects and Ruby classes
Acquisition of JSON data and rotation of values
Convert JSON to TSV and TSV to JSON with Ruby
Created a native extension of Ruby with Rust and published it as a gem
I implemented Ruby with Ruby (and C) (I played with builtin)
Try to link Ruby and Java with Dapr
Manage the version of Ruby itself with rbenv
[Ruby] "Reference to object" and "Contents of variable"
Ruby on Rails ~ Basics of MVC and Router ~
Create jupyter notebook with Docker and run ruby
About call timing and arguments of addToBackStack method
Solving with Ruby and Crystal AtCoder ABC 129 D
Specify the default value with @Builder of Lombok
Mixin test cases with JUnit 5 and default methods
[Ruby / Rails] Inactivate Lint of update_all with Rubocop
[Java] Convert and import file values with OpenCSV
I checked the number of taxis with Ruby
[Ruby] Setting values ​​and memorandum for the table
Basics of Ruby
Ruby and Gem
Feel the basic type and reference type easily with ruby
[Rails] Implementation of drag and drop function (with effect)
Solving with Ruby and Java AtCoder ABC129 D 2D array
Summary of ToString behavior with Java and Groovy annotations
Compatibility of Spring JDBC and MyBatis with Spring Data JDBC (provisional)
[Ruby] Exclude and replace specific patterns with regular expressions
Solving with Ruby, Perl and Java AtCoder ABC 128 C
[Ruby] I made a crawler with anemone and nokogiri.
Build ruby debug environment with VS Code of Windows 10
Feel the basic type and reference type easily with ruby 2
Install rbenv with apt on ubuntu and put ruby
[Technical memo] About the advantages and disadvantages of Ruby
Summary of how to implement default arguments in Java
[Ruby] Class nesting, inheritance, and the basics of self
[Java] [Kotlin] Generically call valueOf and values of Enum
Handling of date and time in Ruby. Use Date and Time properly.