Newbie: assign values/array to variables

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

Hi –

On Wed, 9 May 2007, Mark V wrote:

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?

Yes:

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

David

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

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

Thank you David and Gary!
Regards
Mark

On Wed, May 09, 2007 at 07:41:05PM +0900, Mark V wrote:

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(",")

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