Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Named parameters in Ruby 2

Tags:

ruby

ruby-2.0

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 
like image 548
Alan Coromano Avatar asked Mar 09 '13 06:03

Alan Coromano


People also ask

What are parameters in Ruby?

What is a parameter? Parameters in ruby are variables that are defined in method definition and which represent the ability of a method to accept arguments. So, if we will not have the appropriate parameters, then we will not be able to pass arguments to a method that will contain the data we need.

What is * args in Ruby?

In the code you posted, *args simply indicates that the method accepts a variable number of arguments in an array called args . It could have been called anything you want (following the Ruby naming rules, of course).

How do you use keyword arguments in Ruby?

If you want to accept keyword arguments, in principle you should always use def foo(k: default) or def foo(k:) or def foo(**kwargs) . Note that Ruby 3.0 doesn't behave differently when calling a method which doesn't accept keyword arguments with keyword arguments.

How do you pass an argument in Ruby?

How to Use Command-Line Arguments. In your Ruby programs, you can access any command-line arguments passed by the shell with the ARGV special variable. ARGV is an Array variable which holds, as strings, each argument passed by the shell.


1 Answers

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.

like image 129
AndyV Avatar answered Sep 26 '22 12:09

AndyV