In Ruby, what's the best way to convert a string of the format: "{ 2009, 4, 15 }" to a Date?
views:
826answers:
4
                +1 
                A: 
                
                
              
            Best I could do was:
Date.new(*"{ 2009, 04, 15 }".delete('{').chop.split(",").map(&:to_i))
Is there a more elegant way?
                  jjnevis
                   2010-04-27 11:59:21
                
              
                +1 
                A: 
                
                
              
            Another way:
s = "{ 2009, 4, 15 }"
d = Date.parse( s.gsub(/, */, '-') )
                  FM
                   2010-04-27 12:08:03
                
              
                +7 
                A: 
                
                
              You could also use Date.strptime:
Date.strptime("{ 2009, 4, 15 }", "{ %Y, %m, %d }")
                  robinst
                   2010-04-27 12:12:40
                
              cool :). this is it
                  fl00r
                   2010-04-27 12:18:28
                very nice and very clear - thanks!
                  jjnevis
                   2010-04-27 12:32:54
                
                
                A: 
                
                
              
            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
                  fl00r
                   2010-04-27 12:17:44