Hi, this is actually a Ruby question, but I hope I get an answer anyway. I am looking for a "hash.each.where" function, but I can''t find anything. I have a hash that has a :status key. The value could be ''new'' or ''old''. Now I want to get only the entries where status is ''new''. I know how to do this with an if-clause, but there has to be a better way. What I want is something like this: puts "New entries" hash.each where :status => ''new'' do |new| puts new end puts "Old entries" hash.each where :status => ''old'' do |old| puts old end I thought maybe the hash.select function could help me, but I only found that example and I don''t know how to unse the constraints only for the :status key and not all keys: h = { "a" => 100, "b" => 200, "c" => 300 } h.select {|k,v| k > "a"} #=> {"b" => 200, "c" => 300} h.select {|k,v| v < 200} #=> {"a" => 100} Thanks in advance Sebastian -- 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.
On 30 June 2011 13:27, Sebastian <sebastian.goldt-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> I have a hash that has a :status key. The value could be ''new'' or > ''old''. > > Now I want to get only the entries where status is ''new''. I know how > to do this with an if-clause, but there has to be a better way.Do you mean you have an array of hashes? Or a hash where each value is an object/array? Can you give an illustration of what your hash looks like, so it''s possible to suggest some solutions. Cheers, -- 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.
Sorry, yes I have an array of hashes: hash = [{:name => "like", :status => "old"}, {:name => "this", :status => "new"}, {:name => "here", :status => "old"}] On 30 Jun., 14:31, Michael Pavling <pavl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 30 June 2011 13:27, Sebastian <sebastian.go...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: > > > I have a hash that has a :status key. The value could be ''new'' or > > ''old''. > > > Now I want to get only the entries where status is ''new''. I know how > > to do this with an if-clause, but there has to be a better way. > > Do you mean you have an array of hashes? Or a hash where each value is > an object/array? > Can you give an illustration of what your hash looks like, so it''s > possible to suggest some solutions. > > Cheers,-- 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.
only_new_entries = array_of_hashes.select { |h| h[:status] == "new" } ~ jf -- John Feminella Principal Consultant, BitsBuilder LI: http://www.linkedin.com/in/johnxf SO: http://stackoverflow.com/users/75170/ On Thu, Jun 30, 2011 at 09:07, Sebastian <sebastian.goldt-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> Sorry, > > yes I have an array of hashes: > > hash = [{:name => "like", :status => "old"}, {:name => "this", :status > => "new"}, {:name => "here", :status => "old"}] > > > > > > On 30 Jun., 14:31, Michael Pavling <pavl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> On 30 June 2011 13:27, Sebastian <sebastian.go...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: >> >> > I have a hash that has a :status key. The value could be ''new'' or >> > ''old''. >> >> > Now I want to get only the entries where status is ''new''. I know how >> > to do this with an if-clause, but there has to be a better way. >> >> Do you mean you have an array of hashes? Or a hash where each value is >> an object/array? >> Can you give an illustration of what your hash looks like, so it''s >> possible to suggest some solutions. >> >> Cheers, > > -- > 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. > >-- 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.
John''s answer. Also, the Ruby Hash API<http://www.ruby-doc.org/core/classes/Hash.html> . On Thu, Jun 30, 2011 at 10:10, John Feminella <johnf-u89qwezJ71hz+5FpPkU+UQ@public.gmane.org> wrote:> only_new_entries = array_of_hashes.select { |h| h[:status] == "new" } > > ~ jf > -- > John Feminella > Principal Consultant, BitsBuilder > LI: http://www.linkedin.com/in/johnxf > SO: http://stackoverflow.com/users/75170/ > > > > On Thu, Jun 30, 2011 at 09:07, Sebastian <sebastian.goldt-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> > wrote: > > Sorry, > > > > yes I have an array of hashes: > > > > hash = [{:name => "like", :status => "old"}, {:name => "this", :status > > => "new"}, {:name => "here", :status => "old"}] > > > > > > > > > > > > On 30 Jun., 14:31, Michael Pavling <pavl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> On 30 June 2011 13:27, Sebastian <sebastian.go...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> > wrote: > >> > >> > I have a hash that has a :status key. The value could be ''new'' or > >> > ''old''. > >> > >> > Now I want to get only the entries where status is ''new''. I know how > >> > to do this with an if-clause, but there has to be a better way. > >> > >> Do you mean you have an array of hashes? Or a hash where each value is > >> an object/array? > >> Can you give an illustration of what your hash looks like, so it''s > >> possible to suggest some solutions. > >> > >> Cheers, > > > > -- > > 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. > > > > > > -- > 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. > >-- 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.
Thank you, that was exactly what I was looking for!!! On 30 Jun., 15:16, Paulo Muggler Moreira <paulomugg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> John''s answer. Also, the Ruby Hash > API<http://www.ruby-doc.org/core/classes/Hash.html> > . > > > > > > > > On Thu, Jun 30, 2011 at 10:10, John Feminella <jo...-u89qwezJ71hz+5FpPkU+UQ@public.gmane.org> wrote: > > only_new_entries = array_of_hashes.select { |h| h[:status] == "new" } > > > ~ jf > > -- > > John Feminella > > Principal Consultant, BitsBuilder > > LI:http://www.linkedin.com/in/johnxf > > SO:http://stackoverflow.com/users/75170/ > > > On Thu, Jun 30, 2011 at 09:07, Sebastian <sebastian.go...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> > > wrote: > > > Sorry, > > > > yes I have an array of hashes: > > > > hash = [{:name => "like", :status => "old"}, {:name => "this", :status > > > => "new"}, {:name => "here", :status => "old"}] > > > > On 30 Jun., 14:31, Michael Pavling <pavl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > >> On 30 June 2011 13:27, Sebastian <sebastian.go...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> > > wrote: > > > >> > I have a hash that has a :status key. The value could be ''new'' or > > >> > ''old''. > > > >> > Now I want to get only the entries where status is ''new''. I know how > > >> > to do this with an if-clause, but there has to be a better way. > > > >> Do you mean you have an array of hashes? Or a hash where each value is > > >> an object/array? > > >> Can you give an illustration of what your hash looks like, so it''s > > >> possible to suggest some solutions. > > > >> Cheers, > > > > -- > > > 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. > > > -- > > 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.-- 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.
Sebastian <sebastian.goldt@...> writes:> > Thank you, > > that was exactly what I was looking for!!! >Further to the solution provided, if the data is coming from a database, then you can create some scopes to filter the information for you. It might be faster than looping through the array. -- 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.