【发布时间】:2014-06-01 09:32:48
【问题描述】:
我正在使用 Ruby 2.1.1,并且遇到了三元运算符内部 ruby 块的奇怪行为。请考虑以下程序:
module Prawn
class Document
def self.generate(filename, &block)
block.arity < 1 ? instance_eval(&block) : block[self]
p '---'
block[self]
end
end
end
class A
def initialize
@a = 1
end
def foo
qwe = 1
Prawn::Document.generate("foobar") do
p @a
p qwe
p instance_variables
end
end
end
A.new.foo
让我困惑的是这个程序的输出:
nil
1
[]
"---"
1
1
[:@a]
谁能解释一下为什么“---”上面的结果与“---”下面的结果不同?
【问题讨论】:
标签: ruby block ternary-operator