I am currently facing this question For example, I have this hash array
data = [
{:id => 1,:start_date => "2015-01-02",:end_date => "2015-01-05"},
{:id => 2,:start_date => "2015-01-06",:end_date => "2015-01-07"},
{:id => 3,:start_date => "2015-01-10",:end_date => "2015-01-20"}
]
So I want to find the exact hash that has β2015-01-04β in the range above the start_date and end_date hashes
Follow the document I found out, there are 3 ways to do it
1) Use select
finding_hash = data.select {|h| h[:start_date] <= "2015-01-04" && h[:end_date] >= "2015-01-04"}
find_hash will return an array of the necessary hashes. But as I do this, I assure that there will always be only one hash matching the condition after that SELECT I have finding_hash.firstto get the hash I want
2) Use find
finding_hash = data.find{|h| h[:start_date] <= "2015-01-04" && h[:end_date] >= "2015-01-04"}
This execution method, find_hash IS is the hash result I need
3) Traditional loop
data.each do |t|
if (t[:start_date] <= "2015-01-04" && t[:end_date] >= "2015-01-04")
return t
break
end
end
So which one is the fastest way to do this. I need performance because my data is pretty big!
Thank you and sorry for my bad english!