I am searching through an array of strings looking for a Date:
Is the method I’m using a good way to do it?
OR . . . is there a better alternative.
Perhaps a more “beautiful” way to do it?
query = {'Hvaða','mánaðardagur','er','í','dag?','Það','er','02.06.2011','hví','spyrðu?'}
def has_date(query)
date = nil
query.each do |q|
begin
date = Date.parse(q)
query.delete(q)
break
rescue
end
end
return date
end
Note that in Ruby we use square brackets
[]for array literals (curly braces{}are for Hash literals).Here is a solution that will find all dates in the array and return them as strings (thanks @steenslag):