Overriding instance variable array's operators in Ruby
问题 Sorry for the poor title, I don't really know what to call this. I have something like this in Ruby: class Test def initialize @my_array = [] end attr_accessor :my_array end test = Test.new test.my_array << "Hello, World!" For the @my_array instance variable, I want to override the << operator so that I can first process whatever is being inserted to it. I've tried @my_array.<<(value) as a method in the class, but it didn't work. 回答1: I think you're looking for this: class Test def initialize