From: dblack@... Date: 2006-03-25T20:51:55+09:00 Subject: Re: meaning of *array Hi -- On Sat, 25 Mar 2006, Benjohn Barnes wrote: > That makes a lot of sense, thank you! Usually this will be used to allow a > function to take additional arguments: > > def a_function( required_argument, also_required, *any_optional_arguments ) > puts required_argument > puts also_required > puts any_optional_arguments.join(',') > end > > Calling: >> irb(main):006:0> a_function(1,2,3,4,5) >> 1 >> 2 >> 3,4,5 > > You can also use it to split out an array in to seperate parameters (so kind > of the opposite of the above). Again, this is often used when calling a > method: > >> irb(main):007:0> a=[1,2,3,4,5] >> => [1, 2, 3, 4, 5] >> irb(main):008:0> a_function(*a) >> 1 >> 2 >> 3,4,5 The two uses seem opposite, in a sense, but they're actually the same, if you think of the following equivalency: list,of,things <=> *array In both cases, the star "translates" between an array and a list of things. David -- David A. Black (dblack@wobblini.net) Ruby Power and Light, LLC (http://www.rubypowerandlight.com) "Ruby for Rails" chapters now available from Manning Early Access Program! http://www.manning.com/books/black