Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fooling Ruby's case operator, ===, with proxy objects

I'm trying to make a proxy object that transfers almost all method calls to a child object, essentially the delegator pattern. For the most part, I'm just using a BasicObject and passing every call with method_missing to the child object. So far, so good.

The trick is that try as I might, I can't fool Ruby's case operator, so I can't do:

x = Proxy.new(15)
Fixnum === x #=> false, no matter what I do

This of course makes any case x operations fail, which means the proxies can't be handed off safely to other libraries.

I can't for the life of me figure out what === is using. The proxy works fine for all of the class-based introspection I know of, which is all correctly passed to the child object:

x.is_a?(Fixnum) #=> true
x.instance_of?(Fixnum) #=> true
x.kind_of?(Fixnum) #=> true
x.class #=> Fixnum

Is Module#=== just doing some kind of magic that can't be avoided?

like image 467
bhuga Avatar asked Nov 15 '22 08:11

bhuga


1 Answers

Yeah, it is. Module#=== is implemented in C, examining the object's class hierarchy directly. It doesn't look like there's a way to fool it.

like image 59
mkarasek Avatar answered Jan 03 '23 04:01

mkarasek