Newbie at ruby - syntactic sugar for Range / Array

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
[/code]
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

···

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

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
[/code]
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 :slight_smile: 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 :slight_smile:

David

···

On Sun, 13 Aug 2006, Neil Laurance wrote:

--
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
Ruby for Rails => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.

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

···

On 8/12/06, dblack@wobblini.net <dblack@wobblini.net> wrote:

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

--
Rick DeNatale

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

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

Hi --

···

On Sun, 13 Aug 2006, Rick DeNatale wrote:

On 8/12/06, dblack@wobblini.net <dblack@wobblini.net> wrote:

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

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

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
Ruby for Rails => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.

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

···

On 8/12/06, dblack@wobblini.net <dblack@wobblini.net> wrote:

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

--
Rick DeNatale

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

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

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.

···

On Aug 12, 2006, at 7:39 PM, dblack@wobblini.net wrote:

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

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

_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.

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.

···

On 8/12/06, Rick DeNatale <rick.denatale@gmail.com> wrote:

On 8/12/06, dblack@wobblini.net <dblack@wobblini.net> wrote:

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

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

--
Rick DeNatale

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

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

Hi --

···

On Mon, 14 Aug 2006, Logan Capaldo wrote:

On Aug 12, 2006, at 7:39 PM, dblack@wobblini.net wrote:

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

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
Ruby for Rails => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.

Hi --

···

On Sun, 13 Aug 2006, Matt Todd wrote:

_Why The Lucky Stiff calls it a splat.

I think everyone does, except me :slight_smile: 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
Ruby for Rails => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.

Hi --

···

On Mon, 14 Aug 2006, Rick DeNatale wrote:

On 8/12/06, Rick DeNatale <rick.denatale@gmail.com> wrote:

On 8/12/06, dblack@wobblini.net <dblack@wobblini.net> wrote:

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

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.

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

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
Ruby for Rails => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.