If I have a string such as string = "08/01/2012"
How can I return “2012-08-01” in Ruby in one line?
e.g. take the set of digits after the last “/”, insert them at the beginning, then replace the “/”s with “-“s
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Since you are dealing with dates, I’d go through the
Datelibrary (you have torequire 'date'for some of the features) because it’s the most flexible and stable approach:As you can see both
strptime(“string parse time”) andstrftime(“string format time”) take format strings that exactly describe what they are doing.If you don’t want to go through
Date, you can usegsublike this: