Newbie at ruby - syntactic sugar for Range / Array

N

Neil Laurance

Hi there,

Was hoping someone could point me in the right direction. I'm currently
learning a little ruby, and going through some of the puzzles from the
Ruby Quiz book.

One of the examples uses a very strange looking construct:
Code:
a,b,c,d = *0 .. 3
As far as I understand this, *0 .. 3 equates to (0..3).to_a ? I've been
searching the online Ruby documentation, but couldn't find anwhere that
describes this syntax. Could anyone point me to a description for this?

Many thanks, toolkit
 
D

dblack

Hi --

Hi there,

Was hoping someone could point me in the right direction. I'm currently
learning a little ruby, and going through some of the puzzles from the
Ruby Quiz book.

One of the examples uses a very strange looking construct:
Code:
a,b,c,d = *0 .. 3
As far as I understand this, *0 .. 3 equates to (0..3).to_a ? I've been
searching the online Ruby documentation, but couldn't find anwhere that
describes this syntax. Could anyone point me to a description for this?

The * is the "unar[r]ay" (unary un-array) operator, or at least
so-called by me :) The construct you're talking about is indeed a
little odd. It's one of the times when a range pretends to be an
array. In fact, you don't need to un-array an array to get it to do
parallel assignment:

a,b,c,d = [0,1,2,3]

So *0..3 is almost like saying: you're a range, but pretend that
you're a thing that can be un-arrayed, namely an array. That seems to
be the purpose of the * in this case -- to "trick" the range into
thinking it's an array.

If you look at ranges as array-like lists of values in the first place
(which I don't), then it might make sense in a less convoluted way :)


David

--
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <-----
http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
http://www.manning.com/black => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.
 
R

Rick DeNatale

So *0..3 is almost like saying: you're a range, but pretend that
you're a thing that can be un-arrayed, namely an array. That seems to
be the purpose of the * in this case -- to "trick" the range into
thinking it's an array.

If you look at ranges as array-like lists of values in the first place
(which I don't), then it might make sense in a less convoluted way :)

Or you can think of the * before the last rvalue in an assignment as a
signal to replace the rvalue with a series of rvalues obtained from
the elements of the array resulting from sending to_ary to the
original rvalue.

The original rvalue doesn't need to be an array, or a range, just
anything which responds to to_ary

And actually in Ruby 1.8, it looks like it can be any object, since it
seems to actually use to_a instead of to_ary which is defined in
Object to return an array containing the receiver. But this is
supposed to change in Ruby 1.9

--
Rick DeNatale

IPMS/USA Region 12 Coordinator
http://ipmsr12.denhaven2.com/

Visit the Project Mercury Wiki Site
http://www.mercuryspacecraft.com/
 
D

dblack

Hi --

Or you can think of the * before the last rvalue in an assignment as a
signal to replace the rvalue with a series of rvalues obtained from
the elements of the array resulting from sending to_ary to the
original rvalue.

The original rvalue doesn't need to be an array, or a range, just
anything which responds to to_ary

And actually in Ruby 1.8, it looks like it can be any object, since it
seems to actually use to_a instead of to_ary which is defined in
Object to return an array containing the receiver. But this is
supposed to change in Ruby 1.9

Yes, that's a more cogent explanation, and it accounts for:

a, b = "abc\ndef" => a == "abc\n", b = "def"

So the only weird thing is that ranges can turn themselves into arrays
:)


David

--
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <-----
http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
http://www.manning.com/black => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.
 
M

Matt Todd

_Why The Lucky Stiff calls it a splat. I like this because it's a very
vivid description of a bug-swatter splatting the entrails of the array
(or array-able) object out into separate, individual pieces. From
something compact to its little pieces strewn out in order.

M.T.
 
R

Rick DeNatale

Nothing at all wierd about that, any enumerable can, which makes
perfect sense when you think about it.

And an afterthought.

They don't get turned into anything. The message to_a returns an an
array which represents the receiver, unless the receiver happens to be
an array, in which case it simply returns itself.

--
Rick DeNatale

IPMS/USA Region 12 Coordinator
http://ipmsr12.denhaven2.com/

Visit the Project Mercury Wiki Site
http://www.mercuryspacecraft.com/
 
D

dblack

Hi --

_Why The Lucky Stiff calls it a splat.

I think everyone does, except me :) That's certainly what it was
always called by Perl people 10 or 12 years ago when I first
encountered it in Perl.


David

--
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <-----
http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
http://www.manning.com/black => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.
 
D

dblack

Hi --

And an afterthought.

They don't get turned into anything. The message to_a returns an an
array which represents the receiver, unless the receiver happens to be
an array, in which case it simply returns itself.

Let me rephrase my point: the only weird thing is that ranges are
enumerable :)


David

--
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <-----
http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
http://www.manning.com/black => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.
 
L

Logan Capaldo

Yes, that's a more cogent explanation, and it accounts for:

a, b = "abc\ndef" => a == "abc\n", b = "def"

irb(main):047:0> a, b = "abc\ndef"
=> ["abc\ndef"]
irb(main):048:0> p a
"abc\ndef"
=> nil
irb(main):049:0> p b
nil
=> nil
irb(main):050:0> RUBY_VERSION
=> "1.8.4"

Oh I see, it works with *"abc\ndef".

Weird seeing it there.
 
D

dblack

Hi --

Yes, that's a more cogent explanation, and it accounts for:

a, b = "abc\ndef" => a == "abc\n", b = "def"

irb(main):047:0> a, b = "abc\ndef"
=> ["abc\ndef"]
irb(main):048:0> p a
"abc\ndef"
=> nil
irb(main):049:0> p b
nil
=> nil
irb(main):050:0> RUBY_VERSION
=> "1.8.4"

Oh I see, it works with *"abc\ndef".

Yes -- I forgot the *, which sort of made my point pointless :)


David

--
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <-----
http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
http://www.manning.com/black => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,209
Messages
2,571,088
Members
47,687
Latest member
IngridXxj

Latest Threads

Top