Currently I face this question For example I have this array of hash
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 have "2015-01-04" in range of above hashes's start_date and end_date
Follow the document I find out there are 3 ways to do this
1) Use select
finding_hash = data.select {|h| h[:start_date] <= "2015-01-04" && h[:end_date] >= "2015-01-04"}
finding_hash will return an array of needed hash
But as i do this,i assure that there will always only one hash match the condition do after do this SELECT
i have to finding_hash.first to 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 way of doing ,the finding_hash IS the result hash 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 do need the performance because my data is quite big!
Thank you and sorry for my bad english!