Take the 2-minute tour ×

Ruby 2.0.0 supports keyword arguments (KA) and I wonder what the benefits/use-cases are of this feature in context of pure Ruby, especially when seen in light of the performance penalty due to the keyword matching that needs to be done every time a method with keyword arguments is called.

require 'benchmark'

def foo(a:1,b:2,c:3)

def bar(a,b,c)

number = 1000000
Benchmark.bm(4) do |bm|
  bm.report("foo") { number.times { foo(a:7,b:8,c:9)  } }
  bm.report("bar") { number.times { bar(7,8,9) } }

#           user     system      total        real
# foo    2.797000   0.032000   2.829000 (  2.906362)
# bar    0.234000   0.000000   0.234000 (  0.250010)
share|improve this question
add comment

2 Answers

For example

A function

def welcome_message(message, options={})
  default_options = {name: 'hoge'}
  options = default_options.merge(options)


could be written

def welcome_message(message, name: 'hoge')
share|improve this answer
To add to the answer, use them when you need to assign the arguments to variables inside your method definition and avoid writing custom code yourself. –  Kashyap Feb 25 '13 at 8:17
Don’t cheat us, please. def welcome_message(message, options={:name => 'hoge'}) ; "#{message}、#{options[:name]}" ; end is the one-liner for welcome_message. The main disadvantage of this approach, though, is that one either needs to extend Hash with kinda key_valid? or to hope that keys passed are correct. KA approach lets specify exactly which keys are permitted. –  mudasobwa Feb 25 '13 at 8:52
@mudasobwa Seems to me you are wrong; with your function, welcome_message("message", {:age => 22}) will not show no hoge in the result but will using the keyword. You function set a default value for the whole hash, not a default value for a defined key of the hash. –  oldergod Feb 25 '13 at 9:00
@oldergod Ooups, yes, you are right, sorry. –  mudasobwa Feb 25 '13 at 9:06
@oldergod perhaps I was not very clear in my question. I'm looking for compelling reasons to use this feature. What you have provided is more like a one-off case, giving me a benefit of 2 lines less code and not much more. For example, one distinct advantage of KA's in a language like c# is just have a single method definition and not write tens of different method signatures to implement method overloading whereas in Ruby method overloading is not a valid paradigm so this benefit is not relevant. Another benefit is Code Readability which is relevant to Ruby. –  nonocut Feb 25 '13 at 9:21
add comment

Since KA are ruby-wide innovation, I see two main advantages:

  • limit permitted arguments to a predefined set, as Rails does with assert_valid_keys;
  • use the feature within code blocks.

The summing up:

a = lambda { |name: "Leonardo", age: 67| [name, age] }
a.call # ⇒ ["Leonardo", 67]
a.call name: "Michelangelo", age: 88 # ⇒ ["Michelangelo", 88]
a.call name: "Schwarzenegger", alive: true # ⇒ ArgumentError: unknown keyword: alive
share|improve this answer
add comment

Your Answer


