7

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 the best way? Anyone know what the reason behind these classes not having #new is?

I need a class which behaves like a Fixnum, but has some extra methods, and I'd like to be able to refer to its value through self from within the class, for example:

class Foo < Fixnum
  def initialize value
    super value
  end

  def increment
    self + 1
  end
end

Foo.new(5).increment + 4 # => 10
cloudhead
  • 15,253
  • 6
  • 42
  • 37
  • Tell us what you're really trying to do (the end goal), and we'll try to tell you the best way to do it. I don't think sub-classing is appropriate here. – Matthew Flaschen Jul 08 '09 at 02:05

3 Answers3

17

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.

Yehuda Katz
  • 28,535
  • 12
  • 89
  • 91
  • Hmm. But if you did MyNum.new(2) + MyNum.new(3), wouldn't you get a Fixnum back instead of a MyNum instance? I think you need to wrap @number.send in MyNum.new – Jason C Jul 08 '09 at 03:30
  • I updated my answer to ensure that you return a MyNum if the original response was a Numeric. – Yehuda Katz Jul 08 '09 at 05:11
  • Can MyNum be implemented such that MyNum.new(5) == 5 AND 5 == MyNum.new(5)? – rcrogers Jun 20 '12 at 17:30
4

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.

Jason C
  • 21,377
  • 10
  • 38
  • 33
2

Could you not extend FixNum itself? Like...

class Fixnum
  def even?
    self % 2 == 0
  end
end

42.even?
Turnor
  • 1,836
  • 12
  • 14
  • 1
    I could, but I'd rather not. Also, Fixnum doesn't have #new, and I need to be able to add functionality to #initialize. – cloudhead Jul 08 '09 at 02:19
  • 1
    Monkey-patching should be used carefully. To pollute a Ruby core class just for a specific use case, can be dangerous. – Luca Guidi Jan 15 '14 at 14:52