Named parameters in Ruby 2

RubyRuby 2.0

Ruby Problem Overview


I don't understand completely how named parameters in Ruby 2.0 work.

def test(var1, var2, var3)
  puts "#{var1} #{var2} #{var3}"
end

test(var3:"var3-new", var1: 1111, var2: 2222) #wrong number of arguments (1 for 3) (ArgumentError)

it's treated like a hash. And it's very funny because to use named parameters in Ruby 2.0 I must set default values for them:

def test(var1: "var1", var2: "var2", var3: "var3")
  puts "#{var1} #{var2} #{var3}"
end

test(var3:"var3-new", var1: 1111, var2: 2222) # ok => 1111 2222 var3-new

which very similar to the behaviour which Ruby had before with default parameters' values:

def test(var1="var1", var2="var2", var3="var3")
  puts "#{var1} #{var2} #{var3}"
end

test(var3:"var3-new", var1: 1111, var2: 2222) # ok but ... {:var3=>"var3-new", :var1=>1111, :var2=>2222} var2 var3

I know why is that happening and almost how it works.

But I'm just curious, must I use default values for parameters if I use named parameters?

And, can anybody tell me what's the difference between these two then?

def test1(var1="default value123")
  #.......
end

def test1(var1:"default value123")
  #.......
end

Ruby Solutions


Solution 1 - Ruby

I think that the answer to your updated question can be explained with explicit examples. In the example below you have optional parameters in an explicit order:

def show_name_and_address(name="Someone", address="Somewhere")
  puts "#{name}, #{address}"
end

show_name_and_address
#=> 'Someone, Somewhere'

show_name_and_address('Andy')
#=> 'Andy, Somewhere'

The named parameter approach is different. It still allows you to provide defaults but it allows the caller to determine which, if any, of the parameters to provide:

def show_name_and_address(name: "Someone", address: "Somewhere")
  puts "#{name}, #{address}"
end

show_name_and_address
#=> 'Someone, Somewhere'

show_name_and_address(name: 'Andy')
#=> 'Andy, Somewhere'

show_name_and_address(address: 'USA')
#=> 'Someone, USA'

While it's true that the two approaches are similar when provided with no parameters, they differ when the user provides parameters to the method. With named parameters the caller can specify which parameter is being provided. Specifically, the last example (providing only the address) is not quite achievable in the first example; you can get similar results ONLY by supplying BOTH parameters to the method. This makes the named parameters approach much more flexible.

Solution 2 - Ruby

The last example you posted is misleading. I disagree that the behavior is similar to the one before. The last example passes the argument hash in as the first optional parameter, which is a different thing!

If you do not want to have a default value, you can use nil.

If you want to read a good writeup, see "Ruby 2 Keyword Arguments".

Solution 3 - Ruby

As of Ruby 2.1.0, you no longer have to set default values for named parameters. If you omit the default value for a parameter, the caller will be required to provide it.

def concatenate(val1: 'default', val2:)
  "#{val1} #{val2}"
end

concatenate(val2: 'argument')
#=> "default argument"

concatenate(val1: 'change')
#=> ArgumentError: missing keyword: val2

Given:

def test1(var1="default value123")
  var1
end

def test2(var1:"default value123")
  var1
end

They'll behave the same way when not passed an argument:

test1
#=> "default value123"

test2
#=> "default value123"

But they'll behave much differently when an argument is passed:

test1("something else")
#=> "something else"

test2("something else")
#=> ArgumentError: wrong number of arguments (1 for 0)


test1(var1: "something else")
#=> {:var1=>"something else"}

test2(var1: "something else")
#=> "something else"

Solution 4 - Ruby

I agree with you that it's weird to require default values as the price for using named parameters, and evidently the Ruby maintainers agree with us! Ruby 2.1 will drop the default value requirement as of 2.1.0-preview1.

Solution 5 - Ruby

This is present in all the other answers, but I want to extract this essence.

There are four kinds of parameter:

             Required     Optional
Positional | def PR(a)  | def PO(a=1) |
Keyword    | def KR(a:) | def KO(a:1) |

When defining a function, positional arguments are specified before keyword arguments, and required arguments before optional ones.

irb(main):006:0> def argtest(a,b=2,c:,d:4)
irb(main):007:1> p [a,b,c,d]
irb(main):008:1> end
=> :argtest

irb(main):009:0> argtest(1,c: 3)
=> [1, 2, 3, 4]

irb(main):010:0> argtest(1,20,c: 3,d: 40)
=> [1, 20, 3, 40]

edit: the required keyword argument (without a default value) is new as of Ruby 2.1.0, as mentioned by others.

Solution 6 - Ruby

Leaving this here because it helped me a lot.

Example

Suppose you have this:

def foo(thing, to_print)
  if to_print
    puts thing
  end
end


# this works
foo("hi", true)
# hi
# => nil

so you try adding the argument names, like so:

foo(thing: "hi", to_print: true)
# foo(thing: "hi", to_print: true)
# ArgumentError: wrong number of arguments (given 1, expected 2)
# from (pry):42:in `foo'

but unfortunately it errors.

Solution

Just add a : to the end of each argument:

def foo2(thing:, to_print:)
  if to_print
    puts thing
  end
end


foo2(thing: "hi", to_print: true)
# hi
# => nil

And it works!

Solution 7 - Ruby

According to "Ruby 2.0.0 by Example" you must have defaults:

> In Ruby 2.0.0, keyword arguments must have defaults, or else must be captured by **extra at the end.

Solution 8 - Ruby

def test(a = 1, b: 2, c: 3)
  p [a,b,c]
end

test #=> [1,2,3]
test 10 #=> [10,2,3]
test c:30 #=> [1,2,30] <- this is where named parameters become handy. 

You can define the default value and the name of the parameter and then call the method the way you would call it if you had hash-based "named" parameters but without the need to define defaults in your method.

You would need this in your method for each "named parameter" if you were using a hash.

b = options_hash[:b] || 2

as in:

  def test(a = 1, options_hash)
    b = options_hash[:b] || 2
    c = options_hash[:c] || 3
    p [a,b,c]
  end

Solution 9 - Ruby

You can define named parameters like

def test(var1: var1, var2: var2, var3: var3)
  puts "#{var1} #{var2} #{var3}"
end

If you don't pass one of the parameters, then Ruby will complain about an undefined local variable or method.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlan CoromanoView Question on Stackoverflow
Solution 1 - RubyAndyVView Answer on Stackoverflow
Solution 2 - RubyphoetView Answer on Stackoverflow
Solution 3 - RubytrlinerView Answer on Stackoverflow
Solution 4 - RubyJosh DiehlView Answer on Stackoverflow
Solution 5 - RubyChris CoxView Answer on Stackoverflow
Solution 6 - RubystevecView Answer on Stackoverflow
Solution 7 - RubyMichael FürstenbergView Answer on Stackoverflow
Solution 8 - RubyaaandreView Answer on Stackoverflow
Solution 9 - RubyClemens HelmView Answer on Stackoverflow