Read more

Ruby 2.0 introduces keyword arguments

Dominik Schöler
June 17, 2014Software engineer at makandra GmbH

"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).

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

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

Invoke the method like this:

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:).

Posted by Dominik Schöler to makandra dev (2014-06-17 19:11)