Substitute method arguments in Ruby

how to bind a set of arguments to a variable and then use the variable as argument for a method, e.g.

my_method(arg_a, arg_b, arg_c, arg_d, arg_e)

should be expressed like something like that

my_args = [arg_a, arg_b, arg_c, arg_d, arg_e]
my_method(my_args)

But that doesn't work 'cause my_method expects 5 args but only get the array as one arg. I have few methods with the same arguments set so how to get this working to get DRYer code?

Answers


You should use the splat operator. Try this:

my_args = [arg_a, arg_b, arg_c, arg_d, arg_e]
my_method(*my_args)

Need Your Help

Detect how I run Python script

python windows

How do I detect if a script is run from a Windows console or from Komodo debugger without passing different arguments to the script?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.