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 :-).
There's a lot of good advice in the referenced thread from experienced
Rubyists about why this is not such a good idea in general.
Ruby doesn't require forcing classes to inherit in order to conform to
a type system. Going through hoops to do so is a symptom of thinking
in Java or C++ rather than in Ruby.
···
On 2/12/08, Clifford Heath <no@spam.please.net> wrote:
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 :-).
That's exactly what I want. I also want to be able to do
Year.include module, and Year.new.extend module, adding
instance variables and methods to both the Year class and
year instances, and I can't see any other way to do that,
while maintaining the *appearance* of an integer subclass.
Can you?
There's a lot of good advice in the referenced thread from experienced
Rubyists about why this is not such a good idea in general.
Heh... seven years and counting myself. It's not a good
idea in general. But this is not in general, it's in the
specific case of a data modeling API, where it is a good
idea to makes things *appear* consistent. It means that
my meta-programming doesn't have to include Integer as a
special case in hundreds of locations.
Ruby doesn't require forcing classes to inherit in order to conform to
a type system. Going through hoops to do so is a symptom of thinking
in Java or C++ rather than in Ruby.
All true. I don't really care about the type system, I
care about the POLS which Ruby so freely violates with
Integers. All for good reasons of course, but my hack
doesn't reduce performance of normal integers, it just
makes Ruby work consistently.
Well this doesn't do it. Using the above code but adding a method to Year.
class Year < Integer
def foo
end
end
Year.new(2008).class # => Fixnum
Year.new(2008).foo # =>
# ~> -:18:in `send': undefined method `foo' for 2008:Fixnum (NoMethodError)
# ~> from -:18:in `method_missing'
# ~> from -:35
The point is that Year.new isn't returning a Year which looks like a
Fixnum, it IS returning a Fixnum.
···
On 2/12/08, Clifford Heath <no@spam.please.net> wrote:
Rick DeNatale wrote:
> I don't think this does what you think:
...
> class Year < Integer
> end
>
> Year.new(2008).class # => Fixnum
> Year.new(2008) == 2008 # => true
That's exactly what I want. I also want to be able to do
Year.include module, and Year.new.extend module, adding
instance variables and methods to both the Year class and
year instances, and I can't see any other way to do that,
while maintaining the *appearance* of an integer subclass.
Can you?
The latter gives false with ruby19 though. Also it seems impossible to
define a method in Year.
class Year < Integer
def foo
self * 2000
end
end
Year.new(2008).foo
# NoMethodError: undefined method `foo' for 2008:Fixnum
Also, I don't quite understand the hack with inherited() since it
doesn't do anything. The reason why the class() method returns
'Fixnum' is not because of the inheritance but because of #method_missing. A IMHO slightly more accessible/"traditional" way to
achieve the same would be:
class Year < BasicObject
def initialize(value) @value = value
end
def foo @value * 2000
end
def ==(other) @value == other
end
def respond_to? @value.respond_to?
end
private
def method_missing(meth, *args, &block) @value.send meth, *args, &block
end
end
The latter gives false with ruby19 though. Also it seems impossible to
define a method in Year.
class Year < Integer
def foo
self * 2000
end
end
Year.new(2008).foo
# NoMethodError: undefined method `foo' for 2008:Fixnum
Also, I don't quite understand the hack with inherited() since it
doesn't do anything. The reason why the class() method returns
'Fixnum' is not because of the inheritance but because of #method_missing.
Well you really can define a method in Year, it's just that Year.new
doesn't return an instance of Year, it returns an instance of
BoxedInteger (not Fixnum as I had said earlier.)
A IMHO slightly more accessible/"traditional" way to
achieve the same would be:
class Year < BasicObject
def initialize(value) @value = value
end
def foo @value * 2000
end
def ==(other) @value == other
end
def respond_to? @value.respond_to?
end
private
def method_missing(meth, *args, &block) @value.send meth, *args, &block
end
end