newbie: assign values/array to variables

M

Mark V

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
 
D

dblack

Hi --

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?

Yes:

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


David

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
 
G

Gary Wright

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
 
M

Mark Van De Vyver

Thank you David and Gary!
Regards
Mark

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
 
B

Brian Candler

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top