如何重构以下除非Ruby中的语句

时间:2013-07-17 08:41:54

标签: ruby

在学习Ruby的过程中,我对“除非声明”感到不满。做一个完整的除非声明有时太多了。请重构以下方法以使用单行除非声明

games = ["Super Mario Bros.", "Contra", "Metroid", "Mega Man 2"]
unless games.empty?
  puts "Games in your vast collection: #{games.count}"
end

此代码不会将unless语句更改为内联,请帮我修改此代码以使其可以正常工作,谢谢。

1 个答案:

答案 0 :(得分:1)

然后使用

games = ["Super Mario Bros.", "Contra", "Metroid", "Mega Man 2"]
puts "Games in your vast collection: #{games.count}" unless games.empty?
# >> Games in your vast collection: 4