2017-06-29 32 views
1

編寫一個方法#present_pet,它帶有兩個參數: 寵物名稱和動物類型,都是字符串。它應該返回一個字符串 宣佈寵物(例如,如果給予「託德」和「龜」,它應該返回「託德烏龜已到達。」)。如何在使用兩個字符串進行操作後得到結果中的字符串

def present_pet(pet_name, animal) 
    pet_name.each_line do |pet_name| 
    animal.each_line do |animal| 
     puts pet_name[pet_name] + " " + "the" + " " \ 
      + animal[animal] + " " + "has arrived." 
    end 
    end 
end 

和我運行它後,我得到的只是句子沒有轉換爲字符串!我將如何使它成爲一個字符串?

+0

作爲輸入你期望什麼?你爲什麼'each_line'呢? – mudasobwa

+0

你的方法確實打印了一個字符串。如果你想用雙引號打印,用'p'而不是'puts'。 –

回答

1
# No need to each line if strings. 
def present_pet(pet_name, animal) 
    "#{pet_name} the #{animal} has arrived" 
end 

puts present_pet('Bob', 'builder') 
相關問題