C
Clifford Heath
Folk,
Refer to <http://www.ruby-forum.com/topic/96434> for the context.
I'm doing some metaprogramming where I wanted folk to be able to
subclass Integer, for example: "class Year < Integer; ... end".
You can't do this with Integer in a sensible way, because Integers
are value types in Ruby, and don't support the new() method.
However, I came up with the following cunning bit of code, which
seems to work. The code is also here: <http://pastie.caboo.se/150741>.
I use BasicObject from Facets, which isn't the only way to get that,
you might have 1.9 for example .
Please comment,
Clifford Heath.
require "facets/basicobject" unless Object.const_defined? :BasicObject
class Integer
class BoxedInteger < BasicObject
private
attr_accessor :__value
def self.new(*a)
super *a
end
def initialize(v)
@__value = v.to_i
end
def method_missing(meth, *args, &block)
__value.send meth, *args, &block
end
end
def self.inherited(subclass)
def subclass.new(i)
BoxedInteger.new(i)
end
end
end
Refer to <http://www.ruby-forum.com/topic/96434> for the context.
I'm doing some metaprogramming where I wanted folk to be able to
subclass Integer, for example: "class Year < Integer; ... end".
You can't do this with Integer in a sensible way, because Integers
are value types in Ruby, and don't support the new() method.
However, I came up with the following cunning bit of code, which
seems to work. The code is also here: <http://pastie.caboo.se/150741>.
I use BasicObject from Facets, which isn't the only way to get that,
you might have 1.9 for example .
Please comment,
Clifford Heath.
require "facets/basicobject" unless Object.const_defined? :BasicObject
class Integer
class BoxedInteger < BasicObject
private
attr_accessor :__value
def self.new(*a)
super *a
end
def initialize(v)
@__value = v.to_i
end
def method_missing(meth, *args, &block)
__value.send meth, *args, &block
end
end
def self.inherited(subclass)
def subclass.new(i)
BoxedInteger.new(i)
end
end
end