[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

meaning of *array

Axel Schlueter

3/25/2006 1:55:00 AM

Hi,

When reading somes ruby source I saw the
*array_name notation, an asterisk in front
of an array variables. I was unable to look
up its meaning in the pickaxe. What exactly
does the asterisk do to the array ? Any hints
on where I can find a description in the
pickaxe ?

Kind regards,
Axel
--
Friendship is unnecessary, like philosophy and art. It has no
survival value; rather, it is one of those things that give
value to survival. -- C.S.Lewis


10 Answers

dblack

3/25/2006 2:09:00 AM

0

Axel Schlueter

3/25/2006 2:27:00 AM

0

dblack@wobblini.net wrote:
> The * is a "unary unarray" operator. It "un-arrays" the array into a
> list of separate values.

Ah! Thanks for the explanation.

Kind regards,
Axel
--
Friendship is unnecessary, like philosophy and art. It has no
survival value; rather, it is one of those things that give
value to survival. -- C.S.Lewis

joey__

3/25/2006 7:40:00 AM

0

Axel Schlueter wrote:
> dblack@wobblini.net wrote:
>> The * is a "unary unarray" operator. It "un-arrays" the array into a
>> list of separate values.
>
> Ah! Thanks for the explanation.
>
> Kind regards,
> Axel

It is mostly used as the end param to collect the overflow.

def one(o,t,*args)
puts o
puts t
puts args.join(',')
end

one(1,2,2,2) =>
1
2
2,2

why_ talked about it on his blog a few weeks ago:
http://redhanded.hobix.com/bits/wonderOfTheWhenB...

joey__

--
Posted via http://www.ruby-....


benjohn

3/25/2006 10:34:00 AM

0


On 25 Mar 2006, at 02:09, dblack@wobblini.net wrote:

> Hi --
>
> On Sat, 25 Mar 2006, Axel Schlueter wrote:
>
>> Hi,
>>
>> When reading somes ruby source I saw the
>> *array_name notation, an asterisk in front
>> of an array variables. I was unable to look
>> up its meaning in the pickaxe. What exactly
>> does the asterisk do to the array ? Any hints
>> on where I can find a description in the
>> pickaxe ?

There's something about it near the beginning, in the section about
passing arguments to functions. In fact, until the post below, I had
no idea it could be used more generally!

> The * is a "unary unarray" operator. It "un-arrays" the array into a
> list of separate values.
>
> Thus, for example:
>
> a = [4,5,6]
> [1,2,3,*a] => [1,2,3,4,5,6]
>
> There are rules about where and when and how it can be used, but
> that's basically what's happening when you see that.

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
> => nil
> irb(main):009:0> a_function(a)
> ArgumentError: wrong number of arguments (1 for 2)
> from (irb):9:in `f'
> from (irb):9

As an example of both of these uses; I'd like to have an object
delegate any method calls that it can't handle to some other object.
I could write it like this:

class DelgatingClass
attr_accessor :my_delegatee

...
# Pick method calls I can't handle, and pass them on to the delegatee.
def method_missing( method_name, *all_the_parameters )
@my_delegatee.send( method_name, *all_the_parameters )
end
...
end

:) However, the above doesn't pick up any optional block that's
passed to the method. To rectify that, you would probably write the
method above like this:

def method_missing( method_name, *all_the_parameters, &optional_block )
@my_delegatee.send( method_name, *all_the_parameters, &optional_block )
end

I think this is also discussed in the same section of the pickaxe.

anne001

3/25/2006 10:42:00 AM

0

I used it with opengl. Opengl fx expect arrays of 3 or 4 vectors
(location of points in space, color, etc)

so I entered my data in arrays
scale=[2.0, 0.3, 1.0]
translation=[0.0, 0.0, 0.0]
jointP = [-scale[0]/2, 0, 0]
rotation = [$elbow, 0.0, 0.0, 1.0]
draw = proc { GLUT.WireCube(1.0)}
lowerarm= Node.new(scale,translation, jointP, rotation,&draw)

def initialize(scale, translation, jointP, rotation, &draw)
@children = []
@scale = scale
@Translation = translation
@jointP = jointP
@rotation = rotation
@draw = draw
end

but then when I called GL functions, I had to use *
def applytransform
GL.PushMatrix();
GL.Translate(*@Translation);
if @parent != nil
GL.Translate(*@parentjointP);
end
GL.Rotate(*@rotation);
GL.Translate(*@jointP.minus);
end

Otherwise the GL fx complained it did not get the number of parameters
it wanted.

dblack

3/25/2006 11:52:00 AM

0

Caleb Clausen

3/25/2006 7:42:00 PM

0

On 3/25/06, Benjohn Barnes <benjohn@fysh.org> wrote:
> def method_missing( method_name, *all_the_parameters, &optional_block )
> @my_delegatee.send( method_name, *all_the_parameters, &optional_block )
> end

I like this way better:

require 'forwardable'

class WhateverItIs
extend Forwardable
def_delegator :@my_delegatee, :method_missing
end


benjohn

3/26/2006 9:02:00 AM

0


On 25 Mar 2006, at 19:42, Caleb Clausen wrote:

> On 3/25/06, Benjohn Barnes <benjohn@fysh.org> wrote:
>> def method_missing( method_name, *all_the_parameters,
>> &optional_block )
>> @my_delegatee.send( method_name, *all_the_parameters,
>> &optional_block )
>> end
>
> I like this way better:
>
> require 'forwardable'
>
> class WhateverItIs
> extend Forwardable
> def_delegator :@my_delegatee, :method_missing
> end

:) :) So do I too. It was an example, and in no way intended to show
regular practice!!!! :)

I should have given a link to that when I mentioned it. Caleb is
absolutely right. You should never use the code I posted as anything
more than a guide of how something like that could work, and if you
have been misguided by me, please accept my abject apologies.

You could also look at the Ruby standard library 'delegate',
documentation for which can be found here (Thanks Dave et al):

http://www.rubycentral.com/book/lib_pat...

Cheers,
Benjohn



anne001

3/26/2006 12:26:00 PM

0

here is an example using *
http://www.devsource.com/article2/0,1895,1928...
see the text for explanation of what it does

data = File.new(file_name)
names = data.gets.chomp.split(",") # an array of strings

klass.class_eval do
attr_accessor *names

define_method(:initialize) do |*values|
names.each_with_index do |name,i|
instance_variable_set("@"+name, values[i])
end
end
# more...
end

Caleb Clausen

3/26/2006 4:43:00 PM

0

On 3/26/06, Benjohn Barnes <benjohn@fysh.org> wrote:
> :) :) So do I too. It was an example, and in no way intended to show
> regular practice!!!! :)

I realized this after I posted. It seems like def_delegator(s) isn't
widely enough known, and I thought this could be a good opportunity to
evangalize it, and golf a little. I did sort of miss the point of this
thread by optimizing the * away completely.

> I should have given a link to that when I mentioned it. Caleb is
> absolutely right. You should never use the code I posted as anything
> more than a guide of how something like that could work, and if you
> have been misguided by me, please accept my abject apologies.

Well, you weren't misguided. Either way should work perfectly well. I
would even say your way is probably more commonly found in the wild
than mine.