我有class A
紅寶石重寫<<方法
class A
attr_reader :b
def b=param
@b = param
print "success"
end
end
>> a = A.new
>> a.b = "hello world!"
#> "success"
#> "hello world!"
>> a.b << " and goodbye!"
#> "helo world! and goodbye!"
哪裏是我的 「成功」? :)
我想打印「成功」每當我的變量發生變化。
我不能只是寫
def b<<param
@b << param
print "success"
end
thanx,這是非常有趣的信息 – fl00r 2010-10-11 21:57:50
除了'instance_variable_set',還有'instance_eval'。但這是你不需要使用的黑魔法。 – 2010-10-14 22:36:44