In Ruby, what's the best way to convert a format string: "{ 2009, 4, 15 }" to Date?
"{ 2009, 4, 15 }"
You can also use Date.strptime :
Date.strptime
Date.strptime("{ 2009, 4, 15 }", "{ %Y, %m, %d }")
Another way:
s = "{ 2009, 4, 15 }" d = Date.parse( s.gsub(/, */, '-') )
def parse_date(date) Date.parse date.gsub(/[{}\s]/, "").gsub(",", ".") end date = parse_date("{ 2009, 4, 15 }") date.day #=> 15 date.month #=> 4 date.year #=> 2009
Date.new(*"{ 2009, 04, 15 }".scan(/\d+/).map(&:to_i))