Advanced options and multiple Ruby options

I am trying to set the first argument of a method as optional, followed by any number of arguments. For instance:

def dothis(value=0, *args) 

The problem I am facing is that it does not seem like it is really possible? When I call dothis("hey", "how are you", "good") , I was hoping the default value would be set to 0, but instead it just makes value="hey" . Is there any way to accomplish this behavior?

+4
source share
4 answers

This is not possible in Ruby.

There are many options, though, depending on what you do with your advanced options, and what the method should do.

The obvious choice is

1) Take named parameters using hash syntax

 def dothis params value = params[:value] || 0 list_of_stuff = params[:list] || [] 

Ruby has a nice calling convention around this, you don’t have to provide a hash {} of brackets

 dothis :list => ["hey", "how are you", "good"] 

2) Move the value to the end and take an array for the first parameter

 def dothis list_of_stuff, value=0 

Called as follows:

 dothis ["hey", "how are you", "good"], 17 

3) Use a code block to provide a list

 dothis value = 0 list_of_stuff = yield 

Called as

 dothis { ["hey", "how are you", "good"] } 

4) Ruby 2.0 introduced named hash parameters that handle many options 1, above for you:

 def dothis value: 0, list: [] # Local variables value and list already defined # and defaulted if necessary 

Called the same way as (1):

 dothis :list => ["hey", "how are you", "good"] 
+5
source

This post is a bit outdated, but I want to contribute if someone is looking for a better solution for this. Since ruby ​​2.0 , you can easily do this with named arguments defined using a hash. The syntax is simple and straightforward.

 def do_this(value:0, args:[]) puts "The default value is still #{value}" puts "-----------Other arguments are ---------------------" for i in args puts i end end do_this(args:[ "hey", "how are you", "good"]) 

You can also do the same with the greedy keyword ** args as a hash, for example:

 #**args is a greedy keyword def do_that(value: 0, **args) puts "The default value is still #{value}" puts '-----------Other arguments are ---------------------' args.each_value do |arg| puts arg end end do_that(arg1: "hey", arg2: "how are you", arg3: "good") 
+3
source

You will need to use named parameters to accomplish this:

 def dothis(args) args = {:value => 0}.merge args end dothis(:value => 1, :name => :foo, :age => 23) # => {:value=>1, :name=>:foo, :age=>23} dothis(:name => :foo, :age => 23) # => {:value=>0, :name=>:foo, :age=>23} 
+1
source

using value = 0, you actually assign the value 0. Just to preserve the value, you can either use the above solutions, or simply use the value every time you call this method def dothis (value, digit = [* args]).

The default arguments are used when no arguments are provided.

I ran into a similar problem and I overcame it using:

 def check(value=0, digit= [*args]) puts "#{value}" + "#{digit}" end 

and just call the check like this:

 dothis(value, [1,2,3,4]) 

your value will be the default, and other values ​​relate to a different argument.

0
source

All Articles