Ruby on Rails Thursday, June 30, 2011

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@googlegroups.com.
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.

No comments:

Post a Comment