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 Q. book.

One of the examples uses a very strange looking construct:

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

Hi –

On Sun, 13 Aug 2006, Neil Laurance wrote:

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 8/12/06, [email protected] [email protected] 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


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:

Or you can think of the * before the last rvalue in an assignment as a
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

On 8/12/06, [email protected] [email protected] 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/

On 8/12/06, Rick DeNatale [email protected] wrote:

On 8/12/06, [email protected] [email protected] 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.


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, Matt T. 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

Hi –

On Mon, 14 Aug 2006, Rick DeNatale wrote:

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

On Aug 12, 2006, at 7:39 PM, [email protected] 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”.

Weird seeing it there.

Hi –

On Mon, 14 Aug 2006, Logan C. wrote:

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

Thanks for all the replies, cheers Neil

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs