I have a “BlankState” type of class and it’s method_missing forwards
everything to an instance variable. This way I can treat the object
specially in my own code and all other code doesn’t need to know the
difference. For a contrived example:
class Proxy
remove methods inherited by Object, etc
instance_methods.each do |m|
undef_method m unless m =~ /^__/
end
def initialize name, value
@name, @value = name, value
end
def to_myformat
“#{@name}: #{@value}”
end
def method_missing name, *args, &block
@value.send name, *args, &block
end
end
object = “regular string”
x = Proxy.new(“UID”, object)
x.class # => Array
x.size # => 5
x.to_myformat # => UID: regular string
While that’s a pretty boring example, the idea might come in handy for
something like a graph node, or linked list node (etc). Anyway, the
problem I have is the way this works:
case x
when Array puts “Array”
when Proxy puts “Proxy”
end
This calls Array.===(x) which doesn’t call x.class, and it returns
false. If I redefine Array.===(other) to test self == other.class then
it works. I can’t think of any way to get this to work since it
doesn’t appear there are any methods of x being called that I can
intercept. Does ruby get the class ‘internally’ instead of calling
x.class? I guess if worse comes to worse I can use if x.is_a? Array;
…; elsif x.is_a? Foo; …; end. I don’t want to go about redefining
every class’s === method, that seems to be a mess.
Erwin