2017-10-08 22 views
-1

在練習中,我們必須添加一個名爲「full_name」的方法,它取第一個和最後一個名稱屬性並用空格分隔它們。railstutorial.org 4.4.5練習1

class User 
    attr_accessor :first_name, :last_name, :email 

    def initialize(attributes = {}) 
     @first_name = attributes[:first_name] 
     @last_name = attributes[:last_name] 
     @email = attributes[:email] 
    end 

    def full_name 
     "#{@first_name} #{@last_name}" 
    end 

    def formatted_email 
     full_name "<#{@email}>" 
    end 
end 

我創建了2個獨立的名字和姓氏屬性,我所定義的FULL_NAME方法,我被困在如何實現方法進入「formatted_email」的方法。我試過

"full_name <#{@email}>" 

但是不成功。我應該在哪裏放full_name?

+0

您嘗試寫的內容與您在'formatted_email'的定義中實際寫入的內容不符。 – sawa

+0

'formatted_email'函數讀取爲*調用帶有參數'「<#{email}>」*的'full_name'方法。 – tadman

回答

1
def formatted_email 
    "#{full_name} <#{@email}>" 
end