accessing hash values

  • Thread starter Sean Stephenson
  • Start date
S

Sean Stephenson

Hi all, this is my first post to the Ruby Forum. Cheers. And thanks for
helping out a newb.

Quesiton: How do you access multiple values from a hash?

For Example, I have a hash of computer names, and their associated user.

the_system = {
"computer-01"=>"jill",
"computer-02"=>"jack",
"computer-03"=>"bob"
}


If I want to access the user of one machine, this works great:

puts the_system["computer-01"]
=>jill


But how do I return multiple user names? (The below throws an error:
'wrong number of arguments'.)

puts the_system["computer-01", "computer-02"]


Thanks.
 
J

Joel VanderWerf

Sean said:
But how do I return multiple user names? (The below throws an error:
'wrong number of arguments'.)

puts the_system["computer-01", "computer-02"]

the_system.values_at("computer-01", "computer-02")
 
I

Iñaki Baz Castillo

El Mi=C3=A9rcoles, 2 de Septiembre de 2009, Sean Stephenson escribi=C3=B3:
Hi all, this is my first post to the Ruby Forum. Cheers. And thanks for
helping out a newb.
=20
Quesiton: How do you access multiple values from a hash?
=20
For Example, I have a hash of computer names, and their associated user.
=20
the_system =3D {
"computer-01"=3D>"jill",
"computer-02"=3D>"jack",
"computer-03"=3D>"bob"
}
=20
=20
If I want to access the user of one machine, this works great:
=20
puts the_system["computer-01"]
=3D>jill
=20
=20
But how do I return multiple user names? (The below throws an error:
'wrong number of arguments'.)
=20
puts the_system["computer-01", "computer-02"]

the_system.values_at("computer-01","computer-02")

=3D> ["jill", "jack"]


Note that it returns an Array.


Note: Tested in Ruby1.9 but not in 1.8.


=2D-=20
I=C3=B1aki Baz Castillo <[email protected]>
 
S

Sean Stephenson

Joel said:
Sean said:
But how do I return multiple user names? (The below throws an error:
'wrong number of arguments'.)

puts the_system["computer-01", "computer-02"]

the_system.values_at("computer-01", "computer-02")


AWESOME! Thanks for the quick reply Joel.
 
S

Sean Stephenson

Iñaki Baz Castillo said:
El Miércoles, 2 de Septiembre de 2009, Sean Stephenson escribió:
"computer-03"=>"bob"
'wrong number of arguments'.)

puts the_system["computer-01", "computer-02"]

the_system.values_at("computer-01","computer-02")

=> ["jill", "jack"]


Note that it returns an Array.


Note: Tested in Ruby1.9 but not in 1.8.


And thanks to you as well Iñaki.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top