我有一個測試類和一個盒類,在測試類中,我有一個名爲boxHolder的var,它是一個數組,我想覆蓋該數組的方法<。在單身人士內部,我如何訪問moski_call?在Ruby中的覆蓋實例變量數組的操作符和範圍
class Test attr_accessor :boxHolder def initialize() super self.boxHolder = Array.new class << @boxHolder def <<(box) box.setPositionWithinColumn(moski_call) super(box) end end end def moski_call "YAAAAAAAAAAAAAAAAAAAA" end end class Box def initialize end def setPositionWithinColumn(str) puts "got a string #{str}" end end # test box = Box.new test = Test.new test.boxHolder
按照慣例,rubyists不使用camelCase;它是'box_holder',而不是'boxHolder'等... – 2010-07-22 16:02:08