[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

newbie: assign values/array to variables

mvyver

5/9/2007 10:41:00 AM

Hi,
Thanks for all the fantastic work that has gone into ruby. Even
though I am new it has proved to be very impressive.
To my question.
I've seen the following code:

@vara, @varb = "ab,ac,ad,ae".split(",")

the way @vara and @varb were used makes me think the writer expected
the following:

puts @vara.inspect # "ab"
puts @varb.inspect # ["ac", "ad", "ae"]

However when I run this code I get

puts @vara.inspect # "ab"
puts @varb.inspect # "ac"

Is there a way to achieve the writer seemed to have in mind?

Thanks in advance
Mark


4 Answers

dblack

5/9/2007 10:55:00 AM

0

Gary Wright

5/9/2007 10:57:00 AM

0


On May 9, 2007, at 6:41 AM, Mark V wrote:

> Hi,
> Thanks for all the fantastic work that has gone into ruby. Even
> though I am new it has proved to be very impressive.
> To my question.
> I've seen the following code:
>
> @vara, @varb = "ab,ac,ad,ae".split(",")
>
> the way @vara and @varb were used makes me think the writer expected
> the following:
>
> puts @vara.inspect # "ab"
> puts @varb.inspect # ["ac", "ad", "ae"]

@vara, *@varb = "ab,ac,ad,ae".split(",")

The asterisk (or 'splat') operator is the key.

Gary Wright

mvyver

5/9/2007 11:08:00 AM

0

Thank you David and Gary!
Regards
Mark

On 5/9/07, Gary Wright <gwtmp01@mac.com> wrote:
>
> On May 9, 2007, at 6:41 AM, Mark V wrote:
>
> > Hi,
> > Thanks for all the fantastic work that has gone into ruby. Even
> > though I am new it has proved to be very impressive.
> > To my question.
> > I've seen the following code:
> >
> > @vara, @varb = "ab,ac,ad,ae".split(",")
> >
> > the way @vara and @varb were used makes me think the writer expected
> > the following:
> >
> > puts @vara.inspect # "ab"
> > puts @varb.inspect # ["ac", "ad", "ae"]
>
> @vara, *@varb = "ab,ac,ad,ae".split(",")
>
> The asterisk (or 'splat') operator is the key.
>
> Gary Wright
>
>

Brian Candler

5/9/2007 11:14:00 AM

0

On Wed, May 09, 2007 at 07:41:05PM +0900, Mark V wrote:
> To my question.
> I've seen the following code:
>
> @vara, @varb = "ab,ac,ad,ae".split(",")
>
> the way @vara and @varb were used makes me think the writer expected
> the following:
>
> puts @vara.inspect # "ab"
> puts @varb.inspect # ["ac", "ad", "ae"]
>
> However when I run this code I get
>
> puts @vara.inspect # "ab"
> puts @varb.inspect # "ac"
>
> Is there a way to achieve the writer seemed to have in mind?

@vara, *@varb = "ab,ac,ad,ae".split(",")