Forum: Ruby meaning of *array

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
350ec6ea69f232768f242bf1860af713?d=identicon&s=25 Axel Schlueter (Guest)
on 2006-03-25 02:54
(Received via mailing list)
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
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-03-25 03:09
(Received via mailing list)
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 ?

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.


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
350ec6ea69f232768f242bf1860af713?d=identicon&s=25 Axel Schlueter (Guest)
on 2006-03-25 03:29
(Received via mailing list)
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
6dab365a82517fb694650a57ee88e4a4?d=identicon&s=25 joey__ (Guest)
on 2006-03-25 08:39
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/wonderOfTheWhenBeFlat.html

joey__
Ffcb418e17cac2873d611c2b8d8d891c?d=identicon&s=25 Benjohn Barnes (Guest)
on 2006-03-25 11:33
(Received via mailing list)
On 25 Mar 2006, at 02:09, dblack@wobblini.net wrote:

>> 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:

>         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.
E29eaefee1709918198c8a4f55c42667?d=identicon&s=25 anne001 (Guest)
on 2006-03-25 11:43
(Received via mailing list)
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.
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-03-25 12:52
(Received via mailing list)
Hi --

On Sat, 25 Mar 2006, Benjohn Barnes wrote:

>> irb(main):006:0> a_function(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
Ab870531383eea6e4d9110317f5401e7?d=identicon&s=25 Caleb Clausen (Guest)
on 2006-03-25 20:42
(Received via mailing list)
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
Ffcb418e17cac2873d611c2b8d8d891c?d=identicon&s=25 Benjohn Barnes (Guest)
on 2006-03-26 11:02
(Received via mailing list)
On 25 Mar 2006, at 19:42, Caleb Clausen wrote:

>
> 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_patterns.html

Cheers,
	Benjohn
E29eaefee1709918198c8a4f55c42667?d=identicon&s=25 anne001 (Guest)
on 2006-03-26 14:28
(Received via mailing list)
here is an example using *
http://www.devsource.com/article2/0,1895,1928562,00.asp
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
Ab870531383eea6e4d9110317f5401e7?d=identicon&s=25 Caleb Clausen (Guest)
on 2006-03-26 18:43
(Received via mailing list)
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.
This topic is locked and can not be replied to.