Tutorial by Examples

You define a keyword argument in a method by specifying the name in the method definition: def say(message: "Hello World") puts message end say # => "Hello World" say message: "Today is Monday" # => "Today is Monday" You can define multip...
2.1 Required keyword arguments were introduced in Ruby 2.1, as an improvement to keyword arguments. To define a keyword argument as required, simply declare the argument without a default value. def say(message:) puts message end say # => ArgumentError: missing keyword: message say ...
You can define a method to accept an arbitrary number of keyword arguments using the double splat (**) operator: def say(**args) puts args end say foo: "1", bar: "2" # {:foo=>"1", :bar=>"2"} The arguments are captured in a Hash. You can manip...

Page 1 of 1