Sub-classing Fixnum in ruby

前端 未结 3 1008
猫巷女王i
猫巷女王i 2020-12-15 12:20

So I understand you aren\'t supposed to to directly subclass Fixnum, Float or Integer, as they don\'t have a #new method. Using DelegateClass seems to work though, but is it

相关标签:
3条回答
  • 2020-12-15 12:50

    You can pretty easily set up a quick forwarding implementation yourself:

    class MyNum
      def initialize(number)
        @number = number
      end
    
      def method_missing(name, *args, &blk)
        ret = @number.send(name, *args, &blk)
        ret.is_a?(Numeric) ? MyNum.new(ret) : ret
      end
    end
    

    Then you can add whatever methods you want on MyNum, but you'll need to operate on @number in those methods, rather than being able to call super directly.

    0 讨论(0)
  • 2020-12-15 12:53

    Could you not extend FixNum itself? Like...

    class Fixnum
      def even?
        self % 2 == 0
      end
    end
    
    42.even?
    
    0 讨论(0)
  • 2020-12-15 12:55

    IIRC, the main implementation of Ruby stores Fixnums as immediate values, using some of the low bits of the word to tag it as a Fixnum instead of a pointer to an object on the heap. That's why, on a 32-bit machine, Fixnums are only 29-bits (or whatever it is) instead of a full word.

    So because of that, you can't add methods to a single "instance" of Fixnum, and you can't subclass it.

    If you're dead-set on having a "Fixnum-like" class, you'll probably have to make a class that has a Fixnum instance variable, and forward method calls appropriately.

    0 讨论(0)
提交回复
热议问题