Posted over 8 years ago. Visible to the public. Linked content.

Ruby 2.0 introduces keyword arguments

"Keyword arguments" allow naming method arguments (optionally setting a default value). By using the double-splat operator, you can collect additional options. Default values for standard arguments still work (see adjective).

Copy
def greet(name, adjective = 'happy', suffix: '!', count: 7, **options) greeting = options[:letter] ? 'Dear' : 'Hello' puts "#{greeting} #{adjective} #{name + suffix * count}" end

Invoke the method like this:

Copy
greet('Otto', 'sad', suffix: '??', count: 9, include_blank: true)

In Ruby 2.1+, you can also make an argument required by not giving it a default value: def something(required_option:).

Does your version of Ruby on Rails still receive security updates?
Rails LTS provides security patches for unsupported versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2).

Owner of this card:

Avatar
Dominik Schöler
Last edit:
about 4 years ago
by Dominik Schöler
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Dominik Schöler to makandra dev
This website uses short-lived cookies to improve usability.
Accept or learn more