Hi, I’m just learning ruby. I was searching for date validation in ruby
and found this:
for date in dateArray
if %r{(\d\d)/(\d\d)/(\d\d\d\d)} =~ date
mday, month, year = $1.to_i, $2.to_i, $3.to_i
puts date if Date.valid_civil?(year, month, mday)
else
puts “Invalid Format”
end
end
This seemed to do the job but found out not. The problem is it only
validates the 01/12/2008 but 1/2/2008 fails. Also the date with
different separater fails as well. For 1/2/2008 such dates I tried the
following:
for date in dateArray
if %r{(\d{1,2})/(\d{1,2})/(\d{4})} =~ date
mday, month, year = $1.to_i, $2.to_i, $3.to_i
puts date if Date.valid_civil?(year,month,mday)
else
puts “Invalid Format”
end
end
For some strange reason, it only iterates thru 5 dates though I have
seven dates in dateArray. But for the five it does it right. So, I’m
wondering why? also what can I do to give different options for
separators like “/,:,-,” or space. Though the original code is not my
invention I am wondering what does this line do in particular:
…
mday, month, year = $1.to_i, $2.to_i, $3.to_i
…
Thanks.