开发者

Returning a string according to the arguments

开发者 https://www.devze.com 2023-03-10 21:06 出处:网络
How do I write a function: mymes开发者_开发百科sage(\"2011-02-27\", \"Aravind\") that will return

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
0

精彩评论

暂无评论...
验证码 换一张
取 消