Last active
August 29, 2015 13:56
-
-
Save Najaf/9208344 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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