keyword snippets

Keyword arguments in Ruby +2.1

Tagged argument, keyword, options, ruby  Languages ruby

Before keyword arguments were introduced in Ruby:

def call(options={})
  puts options[:optional_param]
  puts options.fetch(:optional_param_with_default, 'default')
  puts options.fetch(:mandatory_param)
end

After:

def call(mandatory_param:, optional_param: nil, optional_param_with_default: 'default')
  puts optional_param
  puts optional_param_with_default
  puts mandatory_param
end