[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Newbie at ruby - syntactic sugar for Range / Array

Neil Laurance

8/12/2006 8:36:00 PM

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

10 Answers

dblack

8/12/2006 8:44:00 PM

0

Rick DeNatale

8/12/2006 11:21:00 PM

0

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

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

Visit the Project Mercury Wiki Site
http://www.mercuryspace...

dblack

8/12/2006 11:39:00 PM

0

Rick DeNatale

8/13/2006 1:31:00 AM

0

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

Visit the Project Mercury Wiki Site
http://www.mercuryspace...

Matt Todd

8/13/2006 2:28:00 AM

0

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

Rick DeNatale

8/13/2006 10:37:00 PM

0

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.

--
Rick DeNatale

IPMS/USA Region 12 Coordinator
http://ipmsr12.denh...

Visit the Project Mercury Wiki Site
http://www.mercuryspace...

dblack

8/13/2006 11:08:00 PM

0

dblack

8/13/2006 11:10:00 PM

0

Logan Capaldo

8/14/2006 2:27:00 AM

0


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

Weird seeing it there.

dblack

8/15/2006 12:24:00 PM

0