【发布时间】:2012-02-14 16:37:18
【问题描述】:
我有一个自定义类并希望能够覆盖赋值运算符。 这是一个例子:
class MyArray < Array
attr_accessor :direction
def initialize
@direction = :forward
end
end
class History
def initialize
@strategy = MyArray.new
end
def strategy=(strategy, direction = :forward)
@strategy << strategy
@strategy.direction = direction
end
end
这目前无法按预期工作。使用时
h = History.new
h.strategy = :mystrategy, :backward
[:mystrategy, :backward] 被分配给策略变量,方向变量保持为:forward。
重要的是我希望能够为方向参数分配一个标准值。
非常感谢任何使这项工作的线索。
【问题讨论】:
标签: ruby