【发布时间】:2010-09-12 16:48:03
【问题描述】:
我在下面相对简单的分配中遇到了一个有趣的问题。开头的每个带括号的块都评估为 nil,留下 Rubygame::Surface.new 作为应该分配给 @image 的值。不幸的是,在我设置@rect 的下一行,它会抛出一个NoMethodError,因为@image 是nil。
@image = (image unless image.nil?) or
(Rubygame::Surface.autoload(image_file) unless image_file.nil?) or
(Rubygame::Surface.autoload("#{@name}.png") unless @name.nil?) or
Rubygame::Surface.new([16, 16])
@rect = Rubygame::Rect.new [0, 0], [@image.width, @image.height]
类似的测试通过 IRB 运行按预期工作,所以我很确定“或”语句的格式正确,但我无法弄清楚为什么它没有返回新的 Surface,而其他一切都是 无。
【问题讨论】:
-
只有第一行已经让我大吃一惊了...
-
image unless image.nil?->image if image->image