given an Arra tags[] I need to produce a resulting Hash as following .. { "$in" => [tags[0]], "$in" =>[tags[1], ...} in which the key should be always the same and the value being an Array I tried this : myHash = {} tags.each do |tag| h = {"$in" => [tag]} myHash.merge!(h) end but the merge! is only changing the value ... (as the key is always the same ..) (there is no += as with Array class ...) thanks for your feedback -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2011-Feb-27 18:47 UTC
Re: How to store the same key multi times in a Hash ?
On Feb 27, 6:29 pm, Erwin <yves_duf...-ee4meeAH724@public.gmane.org> wrote:> given an Arra tags[] > > I need to produce a resulting Hash as following .. > > { "$in" => [tags[0]], "$in" =>[tags[1], ...} > > in which the key should be always the same and the value being an > Array >By definition, a hash stores a single value for a given key. The closest you''ll get is to make the value an array of all the things for the key in question. Fred -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hasan Iskandar
2011-Feb-27 19:06 UTC
Re: How to store the same key multi times in a Hash ?
I agree with Frederick Cheung. Your code will be like this : myHashes = [] tags.each do |tag| h = {"$in" => [tag]} myHash << h end and myHashes will contains : [{ "$in" => [tags[0]]}, {"$in" =>[tags[1]}, ...] which myHashes is an array of hashes. Hope this can help you :) -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
given an Arra tags[] I need to produce a resulting Hash as following .. { "$in" => [tags[0]], "$in" =>[tags[1], ...} in which the key should be always the same and the value being an Array I tried this : myHash = {} tags.each do |tag| h = {"$in" => [tag]} myHash.merge!(h) end but the merge! is only changing the value ... (as the key is always the same ..) (there is no += as with Array class ...) thanks for your feedback -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
thanks a lot .. !!! On 27 fév, 20:06, Hasan Iskandar <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> I agree with Frederick Cheung. > > Your code will be like this : > > myHashes = [] > tags.each do |tag| > h = {"$in" => [tag]} > myHash << h > end > > and myHashes will contains : > > [{ "$in" => [tags[0]]}, {"$in" =>[tags[1]}, ...] > > which myHashes is an array of hashes. > > Hope this can help you :) > > -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
I was happy too quickly ... I actually need to produce a structure like this ( to be used with Mongoid DB criteria ) { "$in" => [tags[0]], "$in" =>[tags[1]] , ....} I may have to use a json structure ? On 27 fév, 20:16, Erwin <yves_duf...-ee4meeAH724@public.gmane.org> wrote:> given an Arra tags[] > > I need to produce a resulting Hash as following .. > > { "$in" => [tags[0]], "$in" =>[tags[1], ...} > > in which the key should be always the same and the value being an > Array > > I tried this : > > myHash = {} > tags.each do |tag| > h = {"$in" => [tag]} > myHash.merge!(h) > end > > but the merge! is only changing the value ... (as the key is always > the same ..) > (there is no += as with Array class ...) > > thanks for your feedback-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
this is to be used with MongoID as a criteria criteria.where(:tags => { "$in" => [tags[0]], "$in" =>[tags[1]] }).to_a any suggestion with json structure ? thanks Fred On 27 fév, 19:47, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Feb 27, 6:29 pm, Erwin <yves_duf...-ee4meeAH724@public.gmane.org> wrote: > > > given an Arra tags[] > > > I need to produce a resulting Hash as following .. > > > { "$in" => [tags[0]], "$in" =>[tags[1], ...} > > > in which the key should be always the same and the value being an > > Array > > By definition, a hash stores a single value for a given key. The > closest you''ll get is to make the value an array of all the things for > the key in question. > > Fred-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2011-Feb-27 20:49 UTC
Re: How to store the same key multi times in a Hash ?
On Feb 27, 7:56 pm, Erwin <yves_duf...-ee4meeAH724@public.gmane.org> wrote:> this is to be used with MongoID as a criteria > > criteria.where(:tags => { "$in" => [tags[0]], "$in" > =>[tags[1]] }).to_a > > any suggestion with json structure ? >You can''t repeat keys in a json hash either. What are you actually trying to do? Fred> > On 27 fév, 19:47, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > On Feb 27, 6:29 pm, Erwin <yves_duf...-ee4meeAH724@public.gmane.org> wrote: > > > > given an Arra tags[] > > > > I need to produce a resulting Hash as following .. > > > > { "$in" => [tags[0]], "$in" =>[tags[1], ...} > > > > in which the key should be always the same and the value being an > > > Array > > > By definition, a hash stores a single value for a given key. The > > closest you''ll get is to make the value an array of all the things for > > the key in question. > > > Fred-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
I need to write a Mongoid criteria criteria.where(:tags => myStruct ).to_a in which myStruct will have this structure { "$in" => [check_tags[0]], "$in" =>[check_tags[1]] , .. } built from a check_tags Array it''s a sequence of .in criteria I am checking if ALL of the elements of check_tags[] are included in a Mongoid record Array field :tags the simplest Mongoid criteria: criteria.in(:tags => tags) just perform a checking on ANY element included, not ALL On 27 fév, 21:49, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Feb 27, 7:56 pm, Erwin <yves_duf...-ee4meeAH724@public.gmane.org> wrote: > > > this is to be used with MongoID as a criteria > > > criteria.where(:tags => { "$in" => [tags[0]], "$in" > > =>[tags[1]] }).to_a > > > any suggestion with json structure ? > > You can''t repeat keys in a json hash either. What are you actually > trying to do? > > Fred > > > > > > > > > > > On 27 fév, 19:47, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > On Feb 27, 6:29 pm, Erwin <yves_duf...-ee4meeAH724@public.gmane.org> wrote: > > > > > given an Arra tags[] > > > > > I need to produce a resulting Hash as following .. > > > > > { "$in" => [tags[0]], "$in" =>[tags[1], ...} > > > > > in which the key should be always the same and the value being an > > > > Array > > > > By definition, a hash stores a single value for a given key. The > > > closest you''ll get is to make the value an array of all the things for > > > the key in question. > > > > Fred-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Thanks fred .. found how to do it.... there is a specific criteria for it ( I did not fully understand when reading it the first time...) Criteria#all_in: Matches if all values provided match, useful for doing exact matches on arrays. so writing : criteria.all_in(:tags => tags ).to_a did it ... I''ll try to give an eye to the underlaying Mongoid ruby code in charge of doing that .... On 27 fév, 21:49, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Feb 27, 7:56 pm, Erwin <yves_duf...-ee4meeAH724@public.gmane.org> wrote: > > > this is to be used with MongoID as a criteria > > > criteria.where(:tags => { "$in" => [tags[0]], "$in" > > =>[tags[1]] }).to_a > > > any suggestion with json structure ? > > You can''t repeat keys in a json hash either. What are you actually > trying to do? > > Fred > > > > > > > > > > > On 27 fév, 19:47, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > On Feb 27, 6:29 pm, Erwin <yves_duf...-ee4meeAH724@public.gmane.org> wrote: > > > > > given an Arra tags[] > > > > > I need to produce a resulting Hash as following .. > > > > > { "$in" => [tags[0]], "$in" =>[tags[1], ...} > > > > > in which the key should be always the same and the value being an > > > > Array > > > > By definition, a hash stores a single value for a given key. The > > > closest you''ll get is to make the value an array of all the things for > > > the key in question. > > > > Fred-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Jim Ruther Nill
2011-Feb-27 23:58 UTC
Re: Re: How to store the same key multi times in a Hash ?
On Mon, Feb 28, 2011 at 3:53 AM, Erwin <yves_dufour-ee4meeAH724@public.gmane.org> wrote:> I was happy too quickly ... > > I actually need to produce a structure like this ( to be used with > Mongoid DB criteria ) > > { "$in" => [tags[0]], "$in" =>[tags[1]] , ....} > >you can''t. a hash should have unique keys.> I may have to use a json structure ? > > > > On 27 fév, 20:16, Erwin <yves_duf...-ee4meeAH724@public.gmane.org> wrote: > > given an Arra tags[] > > > > I need to produce a resulting Hash as following .. > > > > { "$in" => [tags[0]], "$in" =>[tags[1], ...} > > > > in which the key should be always the same and the value being an > > Array > > > > I tried this : > > > > myHash = {} > > tags.each do |tag| > > h = {"$in" => [tag]} > > myHash.merge!(h) > > end > > > > but the merge! is only changing the value ... (as the key is always > > the same ..) > > (there is no += as with Array class ...) > > > > thanks for your feedback > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- ------------------------------------------------------------- visit my blog at http://jimlabs.heroku.com -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.