How can I inherit from Rational (or any class with no constructor)?

Viewed 336

I can easily inherit from, say, String for example, like this:

class MyString < String
  def stuff
    self + ' and stuff'
  end
end

# This works:
MyString.new('things').stuff # => 'things and stuff'

But how can I inherit from Rational, which has no constructor? For example:

def MyRat < Rational
  def inc
    self + 1
  end
end

# I have tried to initialize like this:
MyRat.new(10).inc # => NoMethodError: undefined method `new' for MyRat:Class
MyRat(10).inc # => NoMethodError: undefined method `MyRat' for main:Object
MyRat.send(:initialize, 10).inc  # => TypeError: already initialized class
# ???
# None of it works!

I can't find a way to initialize my new class.

1 Answers
Related