What does *my_array do?

using tk in ruby, when I add my_array to a listbox it adds all the items as a single large string. when I add them as *my_array it puts them in correctly.

I got *my_array from a example on web, but I don't know what it means.

~S

It expands the array into arguments. For example:

my_array = [ "str1", "str2", "str3" ]
listbox.add(*my_array)

is the same as

listbox.add("str1", "str2", "str3")

Many Ruby APIs make it easier on the developer by automatically expanding the array if it's the lone argument, but I guess tk doesn't, at least not in this case.

-- Brian Palmer

···

On Sep 30, 2005, at 1:56 PM, Shea Martin wrote:

using tk in ruby, when I add my_array to a listbox it adds all the items as a single large string. when I add them as *my_array it puts them in correctly.

I got *my_array from a example on web, but I don't know what it means.

~S

Hi --

using tk in ruby, when I add my_array to a listbox it adds all the items as a single large string. when I add them as *my_array it puts them in correctly.

I got *my_array from a example on web, but I don't know what it means.

It "un-arrays" the array, turning it back into a plain list of
objects. For example:

   a = [3,4,5]
   b = [1,2,*a] # [1,2,3,4,5]

as opposed to:

   b = [1,2,a] # [1,2,[3,4,5]]

It also plays a role in method calls. This:

   def x(*args)

is a method that can take any number of arguments (including zero),
and will store them in the array args.

David

···

On Sat, 1 Oct 2005, Shea Martin wrote:

--
David A. Black
dblack@wobblini.net

Shea Martin wrote:

using tk in ruby, when I add my_array to a listbox it adds all the items as a single large string. when I add them as *my_array it puts them in correctly.

I got *my_array from a example on web, but I don't know what it means.

method(*[1, 2, 3]) is the same as method(1, 2, 3).