我的问题是,我可以通过将第一种方法合并到第二种方法中来简化第二种方法吗?你们会如何做?谢谢!
def happy_birthday(birthday_kids)
# add your code snippet here!
birthday_kids.each do |kids_name, age|
puts "Happy Birthday #{kids_name}! You are now #{age} years old!"
end
end
def age_appropriate_birthday(birthday_kids)
birthday_kids.each do |kid,age|
if age < 13
puts "Happy Birthday #{kid}! You are now #{age} years old!"
else
puts "You are too old for this."
end
end
end
似乎对第二种方法进行了小的修改就可以满足您的要求。
def happy_birthday(birthday_kids, age_appropriate = false)
birthday_kids.each do |kid,age|
if (age < 13 || age_appropriate)
puts "Happy Birthday #{kid}! You are now #{age} years old!"
else
puts "You are too old for this."
end
end
end