Pabi
Pabi

Reputation: 994

Select which argument you want to set when they are more than one default in Ruby

How can I set the second argument in a method with three?

For example:

def method(arg1=5, arg2="something", arg3=12)
end

How would I set the second argument without having to do this:

method(arg1=5, "something_else", arg3=12)

Upvotes: 1

Views: 33

Answers (1)

Soufyane Bouchaala
Soufyane Bouchaala

Reputation: 227

Just use named parameters by using ":" instead of "=", you code can be :

def method(arg1: 5, arg2: "something", arg3: 12)
end

then you can call the method by setting the value for the argument you need :

method(arg2: "something_else")

Upvotes: 2

Related Questions