views:

66

answers:

1

can I code this condition in ruby somehow? I need to know if a string can be converted into a date variable. The only way how to do it is Date.parse("my string") and exception. Is there any other way?

date_scraped_from_the_net = "20 Dec 2009" or it could be "today"

if date_scraped_from_the_net is not a date type 
  needs_to_be_updated = true
end
+5  A: 

If you need sophisticated date parsing, I would try this.

However, if I understand your specific question, you'll want to use rescue to use methods that raise errors in conditionals:

if (Date.parse(date_scraped_from_the_net) rescue nil)
  needs_to_be_updated = true
end
floyd
@floyd: your code is not working for me.I thought there would some other `easier way` how to do it. PS chronic look very goooood.
Radek
I updated the code. There is no easier way I know of.
floyd
@floyd: it is working now.Thank you.I'll wait for a while if anybody else have any idea otherwise I'll accept your answer.
Radek