2017-06-29 20 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') 
相关问题