Hi everybody.
What means this snippet of code? Why use the "star" ?
myarray = [1,2,3,4,5,6,6]
qa = *myarray
Thank you.
···
--
-- Mirko Viviani --
GPG-PGP Public Key: 0xE4E8FAB1
Fingerprint: 14D3 A373 E926 7737 DF32 502B A4C4 1CE2 E4E8 FAB1
***********************************************
"“Machines take me by surprise with great frequency.” A. Turing
Hi everybody.
What means this snippet of code? Why use the "star" ?
myarray = [1,2,3,4,5,6,6]
qa = *myarray
Thank you.
In this example it doesn't really do anything, i think. Normally you
would use the * in an argument list, in a method definition, to
effectively say 'pull all arguments after this into a single array', eg
def foo(mainarg, *otherargs)
puts mainarg.inspect
puts otherargs.inspect
end
=> nil
foo(1,2,3,4,5,6)
1
[2, 3, 4, 5, 6]
=> nil
In this sense it's like * in a regular expression, meaning 'any number
of arguments'. That's how i understand it anyway, a more precise
description will probably be forthcoming
Hi everybody.
What means this snippet of code? Why use the "star" ?
myarray = [1,2,3,4,5,6,6]
qa = *myarray
In addition to the examples in the other answers, note that the star
has some new behavior in 1.9.
Here's the 1.8 behavior.
*a = 1; a
=> [1]
*a = [1]; a
=> [[1]]
*a = [[1]]; a
=> [[[1]]]
In each case, the star indicates one "missing" level of array
wrapping. In 1.9, that's changed:
*a = 1; a
=> [1]
*a = [1]; a
=> [1]
*a = [[1]]; a
=> [[1]]
*a = 1 and *a = [1] have the same result. (I know there was a lot of
discussion about this when it was being changed but I can't remember
exactly what the rationale was.)
Sorry,
Complete code was like this
x, *xs = *myarray
after executing, x is 1 and xs [2,3,4,5,6,6] ..... :o
···
2009/7/23 Max Williams <toastkid.williams@gmail.com>
Mirko Viviani wrote:
> Hi everybody.
> What means this snippet of code? Why use the "star" ?
>
> myarray = [1,2,3,4,5,6,6]
> qa = *myarray
>
> Thank you.
In this example it doesn't really do anything, i think. Normally you
would use the * in an argument list, in a method definition, to
effectively say 'pull all arguments after this into a single array', eg
In this sense it's like * in a regular expression, meaning 'any number
of arguments'. That's how i understand it anyway, a more precise
description will probably be forthcoming
--
Posted via http://www.ruby-forum.com/\.
--
-- Mirko Viviani --
GPG-PGP Public Key: 0xE4E8FAB1
Fingerprint: 14D3 A373 E926 7737 DF32 502B A4C4 1CE2 E4E8 FAB1
***********************************************
"“Machines take me by surprise with great frequency.” A. Turing
Sorry,
Complete code was like this
x, *xs = *myarray
after executing, x is 1 and xs [2,3,4,5,6,6] ..... :o
Mirko Viviani wrote:
Hi everybody.
What means this snippet of code? Why use the "star" ?
myarray = [1,2,3,4,5,6,6]
qa = *myarray
Thank you.
In this example it doesn't really do anything, i think. Normally you
would use the * in an argument list, in a method definition, to
effectively say 'pull all arguments after this into a single array', eg
def foo(mainarg, *otherargs)
puts mainarg.inspect
puts otherargs.inspect
end
=> nil
foo(1,2,3,4,5,6)
1
[2, 3, 4, 5, 6]
=> nil
In this sense it's like * in a regular expression, meaning 'any number
of arguments'. That's how i understand it anyway, a more precise
description will probably be forthcoming
--
Posted via http://www.ruby-forum.com/\.
The * is also called the "splat operator". It is used to split up an array in is parts or
the other way round (like in the method definition already posted). You could also
do things like:
Sorry,
Complete code was like this
x, *xs = *myarray
after executing, x is 1 and xs [2,3,4,5,6,6] ..... :o
Ah, well there you go. In ruby, you can assign multiple variables to
multiple values simultaneously: effectively on the left side of the =
you have a list of variables and on the right you have a list of values.
If the sizes of the lists don't match up then any extra values are
thrown away and any extra variables are set to nil. eg
However, if you use the * on the rightmost variable, then any extra
values get pulled into an array and that variable points to that array:
a, b, c = 1, 2, 3
=> [1, 2, 3]
puts a.inspect; puts b.inspect; puts c.inspect
1
2
3
=> nil
a, b, c = 1, 2
=> [1, 2]
puts a.inspect; puts b.inspect; puts c.inspect
1
2
nil
=> nil
a, b = 1, 2, 3
=> [1, 2, 3]
puts a.inspect; puts b.inspect
1
2
=> nil
a, *b = 1, 2, 3
=> [1, 2, 3]
puts a.inspect; puts b.inspect
1
[2, 3]
···
2009/7/23 Max Williams <toastkid.williams@gmail.com>
thank's to all.... this is a think I would remember
···
2009/7/23 7stud -- <bbxx789_05ss@yahoo.com>
Mirko Viviani wrote:
> Sorry,
> Complete code was like this
> x, *xs = *myarray
>
> after executing, x is 1 and xs [2,3,4,5,6,6] ..... :o
>
> 2009/7/23 Max Williams <toastkid.williams@gmail.com>
x, y, z = 1, 2, 3
puts x, y, z
--output:--
1
2
3
x, y, z = *[1, 2, 3]
puts x, y, z
--output:--
1
2
3
*y = 1, 2, 3
p y
--output:--
[1, 2, 3]
x, *y = *[1, 2, 3]
puts x
p y
--output:--
1
[2, 3]
Depending on the context the * means:
a) replace an array with its individual elements
b) gather individual elements into an array
--
Posted via http://www.ruby-forum.com/\.
--
-- Mirko Viviani --
GPG-PGP Public Key: 0xE4E8FAB1
Fingerprint: 14D3 A373 E926 7737 DF32 502B A4C4 1CE2 E4E8 FAB1
***********************************************
"“Machines take me by surprise with great frequency.” A. Turing
ruby is automatically starring just the right side of the expression,
under the hood? eg
In pickaxe2, p. 91, the rule is:
···
----
When an assignment has more than one lvalue, the assignment expression
returns an array of the rvalues. Testing that out:
----
result = (x, y = [1], [2, 3])
p result
So I guess you could say "ruby is automatically starring the right hand
side of a parallel assignment if there is more than one lvalue, and the
resulting array is the value of the entire expression.
And I guess this wasn't a very good example:
x, y, z = *[1, 2, 3]
puts x, y, z
--output:--
1
2
3
Because you can eliminate the star and get the same output:
x, y, z = [1, 2, 3]
p x
p y
p z
--output:--
1
2
3
But this is where the star makes a difference:
x, y, z = 1, [2, 3]
p x
p y
p z
--output:--
1
[2, 3]
nil
x, y, z = 1, *[2, 3]
p x
p y
p z
--output:--
1
2
3
(This applies to ruby 1.8. See David Black's post for 1.9 behavior.)
--
Posted via http://www.ruby-forum.com/\.