Home > Net >  How to make method parameters and arguments optional in Ruby?
How to make method parameters and arguments optional in Ruby?

Time:09-18

I have a situation where I'm dealing with couple of methods.I'm confused how to pass parameters and arguments for them. I initially have these two methods let's say -

def method_foo1 classes_prod, classes_corp, run_user, application
set_name_prod = ... // declaring variable
all_classes_prod = ... //declaring variable
method_foo3 set_name_prod, all_classes_prod, run_user , application //calling method 3
end

I'm introducing a second method which also calls method_foo3 from body of it but now it should pass only three arguments.Something like below -

def method_foo2 account_id,application
account_id_prod = .... // declaring variable
method_foo3 account_id_prod, application // //calling method 3
end 

Now the main method/ method_3 which method_1 & method_2 are calling -

def method_3 set_name,classes,run_user,application
..... //body of method_3
end

As, we can see the method_3 has only 4 parameters available previously. Now I want to introduce a parameter "account_id" along with the rest. And make sure "classes" , "run_user" & new parameter "account_id" optional as they're not mandatorily being called from method_1 and method_2. How can I do this ?Any help is really appreciated.

I basically want to skip few parameters when they don't get any value and make sure the exact argument goes to exact parameter.

CodePudding user response:

As far as I know, Ruby has two distinct ways of letting method parameters be optional:

  • default values
  • named arguments

Unfortunately, unlike Python, they don't mix - a parameter is either required, or has a default value, or is named.

For instance:

def method_3 set_name = "", classes = "", run_user = "", application = "", account_id: ""
end

You can call this in many ways though:

  • method_3 -- all default values
  • method_3("my set") -- default values for all but set_name
  • method_3("my set", account_id: "account") -- mixed, set_name and account_id are set
  • method_3(account_id: "account") -- default values for all but account_id`

CodePudding user response:

I didn't quite understand what the question was about exactly so I will answer in general, hope this helps

Using a large number of parameters in methods is always a pain. It's very hard to maintain, it's very hard to pass arguments, because it's easy to get confused in them

Also complicating the problem is the fact that there is no method overload in Ruby. You can't have methods with same names but with different parameters number, every new method will override old one

That's the reason, why it is much more convenient to use hashes as parameters

For example

def foo(args)
  puts args[:bar]
end
foo(bar: "bar")
# will print
# bar

If after that you decide to use some new parameter, it doesn't matter. It's very easy to add

def foo(args)
  puts args[:bar]
  puts args[:baz]
end
foo(bar: "bar", baz: "baz")
# will print
# bar
# baz

You can combine hash with kwargs and (or) default kwargs like in Rob Spoor examples

def foo(boo: "boo", **args)
  puts args[:bar]
  puts args[:baz]
  puts boo
end
foo(bar: "bar", baz: "baz")
# will print
# bar
# baz
# boo
foo(bar: "bar", baz: "baz", boo: "baa")
# will print
# bar
# baz
# baa

I also want to note (although it is opinion based) the fact that it is considered good practice to use parentheses when defining a method and when calling it, this significantly improves the readability of the code

  • Related