我有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