1

在学习 Ruby 的过程中,我遇到了 unless 语句。做一个完整的除非声明有时太多了。请重构下面的方法以使用单行除非语句

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

此代码不会将unless语句更改为内联,请帮助我修改此代码以便它可以工作,谢谢。

4

1 回答 1

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
于 2013-07-17T08:44:15.047 回答