【问题标题】:Overriding instance variable array's operators in Ruby在 Ruby 中覆盖实例变量数组的运算符
【发布时间】:2010-12-11 23:34:27
【问题描述】:

对不起,标题很差,我真的不知道该怎么称呼它。

我在 Ruby 中有这样的东西:

class Test
  def initialize
    @my_array = []
  end
  attr_accessor :my_array
end
test = Test.new
test.my_array << "Hello, World!"

对于@my_array 实例变量,我想覆盖&lt;&lt; 运算符,以便我可以首先处理插入其中的任何内容。我已经尝试将@my_array.&lt;&lt;(value) 作为类中的一种方法,但它不起作用。

【问题讨论】:

    标签: ruby operators operator-overloading


    【解决方案1】:

    我不确定这实际上是你可以直接做的事情。

    您可以尝试从Array 创建一个派生类,实现您的功能,例如:

    class MyCustomArray < Array
      def initialize &process_append
        @process_append = &process_append
      end
      def << value
        raise MyCustomArrayError unless @process_append.call value
        super.<< value
      end
    end
    
    class Test
      def initialize
        @my_array = MyCustomArray.new
      end
      attr_accessor :my_array
    end
    

    【讨论】:

    • 其实我会做 Glenn McDonald 的回答。
    【解决方案2】:
    a = []
    a.instance_eval("alias old_add <<; def << value; puts value; old_add(value); end")
    

    非常骇人听闻,我想不通......

    只要用你想做的任何预处理来改变“puts value”。

    【讨论】:

      【解决方案3】:

      给你...

      $ cat ra1.rb
      
      class Aa < Array
        def << a
          puts 'I HAVE THE CONTROL!!'
          super a
        end
      end
      
      class Test
        def initialize
          @my_array = Aa.new
        end
        attr_accessor :my_array
      end
      
      test = Test.new
      test.my_array << "Hello, World!"
      puts test.my_array.inspect
      $ ruby ra1.rb
      I HAVE THE CONTROL!!
      ["Hello, World!"]
      $ 
      

      【讨论】:

        【解决方案4】:

        您可以扩展任何单个对象的元类,而无需创建一个全新的类:

        >> i = []
        => []
        >> class << i
        >>   def <<(obj)
        >>     puts "Adding "+obj.to_s
        >>     super
        >>   end
        >> end
        => nil
        >> i << "foo"
        Adding foo
        => ["foo"]
        

        【讨论】:

          【解决方案5】:

          我想你正在寻找这个:

          class Test
            def initialize
              @myarray = []
              class << @myarray
                def <<(val)
                  puts "adding #{val}" # or whatever it is you want to do first
                  super(val)
                end
              end
            end
            attr_accessor :myarray
          end
          

          Understanding Ruby Singleton Classes 上有一篇关于这个和相关主题的好文章。

          【讨论】:

            【解决方案6】:

            我扩展了类,创建了一个方法来提供对实例变量的访问。

                    class KeywordBid
                      def override_ignore_price(ignore_price)
                        @ignorePrice = ignore_price
                      end
                    end
            

            【讨论】:

              猜你喜欢
              • 2011-03-19
              • 2022-11-17
              • 1970-01-01
              • 2011-03-20
              • 2014-11-29
              • 1970-01-01
              • 2011-06-01
              • 1970-01-01
              • 2013-01-27
              相关资源
              最近更新 更多