生日快乐方法-简单方法-RUBY

问题描述 投票:0回答:1

我的问题是,我可以通过将第一种方法合并到第二种方法中来简化第二种方法吗?你们会如何做?谢谢!

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
ruby
1个回答
0
投票

似乎对第二种方法进行了小的修改就可以满足您的要求。

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
© www.soinside.com 2019 - 2024. All rights reserved.