所以我理解你不应该直接将Fixnum,Float或Integer子类化,因为它们没有#new方法.使用DelegateClass似乎工作,但它是最好的方法吗?有谁知道这些课没有#new背后的原因是什么?
我需要一个类似Fixnum的类,但有一些额外的方法,我希望能够self从类中引用它的值,例如:
class Foo < Fixnum
def initialize value
super value
end
def increment
self + 1
end
end
Foo.new(5).increment + 4 # => 10
Run Code Online (Sandbox Code Playgroud)