English 中文(简体)
How can I assign multiple values to a hash key?
原标题:
  • 时间:2009-11-06 13:14:47
  •  标签:
  • ruby
  • hash

For the sake of convenience I am trying to assign multiple values to a hash key in Ruby. Here s the code so far

myhash = { :name => ["Tom" , "Dick" , "Harry"] }

Looping through the hash gives a concatenated string of the 3 values

Output:

name : TomDickHarry

Required Output:

:name => "Tom" , :name => "Dick" , :name => "Harry"

What code must I write to get the required output?

最佳回答

You ve created a hash with the symbol name as the key and an array with three elements as the value, so you ll need to iterate through myhash[:name] to get the individual array elements.

问题回答
myhash.each_pair {|k,v| v.each {|n| puts "#{k} => #{n}"}}
#name => Tom
#name => Dick
#name => Harry

The output format is not exactly what you need, but I think you get the idea.

The answers from Rohith and pierr are fine in this case. However, if this is something you re going to make extensive use of it s worth knowing that the data structure which behaves like a Hash but allows multiple values for a key is usually referred to as a multimap. There are a couple of implementations of this for Ruby including this one.

re: the issue of iterating over selective keys. Try using reject with the condition inverted instead of using select.

e.g. given:

{:name=>["Tom", "Dick", "Harry"], :keep=>[4, 5, 6], :discard=>[1, 2, 3]}

where we want :name and :keep but not :discard

with select:

myhash.select { |k, v| [:name, :keep].include?(k) }
=> [[:name, ["Tom", "Dick", "Harry"]], [:keep, [4, 5, 6]]]

The result is a list of pairs.

but with reject:

myhash.reject { |k, v| ![:name, :keep].include?(k) }
=> {:name=>["Tom", "Dick", "Harry"], :keep=>[4, 5, 6]}

The result is a Hash with only the entries you want.

This can then be combined with pierr s answer:

hash_to_use = myhash.reject { |k, v| ![:name, :keep].include?(k) }
hash_to_use.each_pair {|k,v| v.each {|n| puts "#{k} => #{n}"}}




相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

热门标签