How can I do the dictionary?

I am a newbie but I’m familial with C#.
I can’t find any dictionary class in Ruby.
I need to do like Dictionary<key, List>

Can I do that in Ruby? If yes, how?
Big thanks!

I think what you seek is Hash.

On Apr 19, 2011, at 5:47 PM, Siratinee S. wrote:

I am a newbie but I’m familial with C#.
I can’t find any dictionary class in Ruby.
I need to do like Dictionary<key, List>

Can I do that in Ruby? If yes, how?
Big thanks!


Posted via http://www.ruby-forum.com/.

Best regards,
Zhi-Qiang L.
[email protected]

Siratinee S. wrote in post #993714:

I am a newbie but I’m familial with C#.
I can’t find any dictionary class in Ruby.
I need to do like Dictionary<key, List>

Can I do that in Ruby? If yes, how?
Big thanks!

my_hash = {
‘fruits’ => [‘apple’, ‘pear’],
‘cars’ => [‘VW’, ‘BMW’, ‘Ford’]
}

p my_hash[‘cars’]

–output:–
[“VW”, “BMW”, “Ford”]

my_hash[10] = [1, 2, 3]
p my_hash

–output:–
{“fruits”=>[“apple”, “pear”], “cars”=>[“VW”, “BMW”, “Ford”], 10=>[1, 2,
3]}

In ruby, keys and values for a hash can be anything, and you can freely
mix types. In ruby 1.9, hashes are now ordered–in the order the keys
were added.