我正在寻找一些方法链接。我有以下代码:
class MyClass
attr_accessor :handler
def do_a
puts 'i just did a'
self.handler = 'a'
self
end
def do_b_if_a
puts 'i just did b' if handler == 'a'
end
end
所以以下工作:
irb > test = MyClass.new
=> #<MyClass:0x007fa44ced9a70 @handler=nil>
irb > test.do_a
'i just did a'
irb > test.do_a.do_b_if_a
'i just did a'
'i just did b'
我不想工作的是当我第一次调用 do_a 它设置处理程序时,这意味着现在可以随时调用 do_b_if_a。但我只希望它在与 do_a 链接时被调用,我该怎么做?