From: Brian Candler on 10 Aug 2010 08:48 Dmitriy Makarov wrote: >> do you need a multivalue hash? >> {key => val1, val2, val3 ...} > > Yes (1) myhash = Hash.new { |h,k| h[k] = [] } myhash['key'] << 'val1' myhash['key'] << 'val2' myhash['key'] << 'val3' p myhash # {"key"=>["val1", "val2", "val3"]} p myhash['key'] # ["val1", "val2", "val3"] (2) myhash = Hash.new { |h,k| h[k] = {} } myhash['key']['val1'] = true myhash['key']['val2'] = true myhash['key']['val3'] = true p myhash # {"key"=>{"val3"=>true, "val1"=>true, "val2"=>true}} p myhash['key'].keys # ["val3", "val1", "val2"] The latter has the property that duplicate values are eliminated, and it's very quick to test for a particular value: if myhash['key']['val1'] ... etc end -- Posted via http://www.ruby-forum.com/.
From: a.bovanenko on 10 Aug 2010 10:43 Hi. You cannot store in Java hash map several pairs with the same keys -----Original Message----- From: jason joo Sent: 10.08.2010, 13.28 To: ruby-talk ML Subject: Re: hash another way is to make a patch to Hash object, put ur own rules in it and u will have a HashMap in ruby 2010/8/10 Peter Hickman <peterhickman386(a)googlemail.com> > The Ruby hash is not a Java HashMap > > x["key"] = "value_1" > x["key"] = "value_2" > > puts x["key"] => "value_2" > > The best you can do is: > 1) Create a HashMap class for Ruby, a very simple task > 2) Use lists to store the values > > x["key"] = Array.new > x["key"] << "value_1" > x["key"] << "value_2" > > puts x["key"] => ["value_1", "value_2"] > > On 10 August 2010 10:05, Dmitriy Makarov <makarovx(a)gmail.com> wrote: > >> what do u mean by 'repeat'? > > > > axample in java > > HashMap h=ne HashMap(); > > h.put("key","value_1"); > > h.put("key","value_2"); > > h.put("key","value_N"); > > > > how to made in ruby > > > > > > > > -- > > Posted via http://www.ruby-forum.com/. > > > > > >
First
|
Prev
|
Pages: 1 2 3 Prev: [ANN] Detest 3.1.2 Next: Determining whether the running ruby is outdated? |