2011-06-04 81 views
0

我怎樣寫一個函數:返回根據參數的字符串

mymessage("2011-02-27", "Aravind") 

,將返回

你好亞拉文,您輸入以下日期:週五2011年2月27日

+0

你是新紅寶石呢?我建議你先學習ruby,這樣你就有了一個很好的基礎來打造你的rails知識。 – 2011-06-04 05:47:37

回答

4

好吧,Rails使用模型視圖控制器設計模式,所以理想情況下,要做到這一點,您需要在所有三個組件中創建代碼。

爲了簡單起見,我會假設你想在一個幫手裏做這個,而且我只會寫這個方法。首先,來看看如何可以構造日期:

ROR + Ruby Date in Different Format

現在,你的函數可以像:

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 
+0

也許最好把這種方法放在模型中? – 2011-06-04 05:45:36

+0

這就是我如何開始我的回答:) – Spyros 2011-06-04 05:46:26

+0

更好的是我的解決方案,當我忘記strftime時,我需要更多的咖啡。 – 2011-06-04 05:47:48

1
def mymessage(date,name) 
    "Hello #{name}, you entered the following date: #{date}" 
end