Skip to content

Instantly share code, notes, and snippets.

@Najaf
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Najaf/9208344 to your computer and use it in GitHub Desktop.
Save Najaf/9208344 to your computer and use it in GitHub Desktop.
def this_method_takes_three_arguments(a, b, c)
# do stuff with three arguments
end
# Now imagine you had an array that contained the three arguments that
# you wanted to pass into the above method:
arguments = ['foo', 'bar', 'baz']
# You could pass them in like this:
this_method_takes_three_arguments(arguments[0], arguments[1], arguments[2])
# But this will work just as well:
this_method_takes_three_arguments(*arguments)
# This works the other way around as well. If you have a method that is defined like this:
def this_method_takes_a_variable_number_of_arguments(*args)
args.each do |a|
# do stuff with the argument
end
end
# You can then call it with however many arguments you want:
this_method_takes_a_variable_number_of_arguments('foo', 'bar', 'baz', 'monkey', 'banana', 'pineapple', 'whatever')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment