From: Max Williams Date: 2009-07-11T23:02:49+09:00 Subject: A clean way to edit a time? Hi all I have a method, created_at=, which takes a time or a string, and changes a Time field, created_at, accordingly. If we get a time, then it's easy: just set created_at to be the given time. However, if we get a string, it's going to be only a date, like "11/07/09", and in this case i want to update only the day, month and year of created_at, and leave the rest alone. In some cases, like when a new record is made, then there will be no value in created_at, so the method should cope with the current value of created_at being nil (i do this by setting the time to Time.now before updating it with the new values). Currently i have this which feels like a bit of a hacky mess. Can anyone show me a cleaner solution? def created_at=(time) if time.kind_of?(Time) self[:created_at] = time elsif time.kind_of?(String) begin parsed_time = DateTime.strptime(time, "%d/%m/%y") old_time = self.created_at || Time.now return self[:created_at] = Time.local(parsed_time.year, parsed_time.month, parsed_time.day, old_time.hour, old_time.min, old_time.sec) rescue return false end end end Also, i can't get it to return the new time value, and it doesn't return false if it raises the exception. In either case it just returns the string that i passed to it. It's not vital that it return the new created_at value (or false) but it would be nice. I'm doing this in a framework based on Ramaze so maybe the last point is due to something inherited from that. thanks! max -- Posted via http://www.ruby-forum.com/.