Returning a string according to the arguments
How do I write a function:
mymes开发者_开发百科sage("2011-02-27", "Aravind")
that will return
Hello Aravind, you entered the following date: Friday February 27, 2011
Well, Rails works with a model view controller design pattern, so ideally, to do this, you would need to create code in all three components.
For simplicity, i will assume that you want to do this inside a helper, and i will only write the method. First of all, take a look at how you can construct the date :
ROR + Ruby Date in Different Format
Now, your function could be like :
def mymessage(the_date, name)
formatted_date = the_date.to_time.strftime("%A %B %d, %Y")
"Hello #{name}, you entered the following date: #{formatted_date}"
end
def mymessage(date,name)
"Hello #{name}, you entered the following date: #{date}"
end
精彩评论