Let’s be evil, and fake out
false with some metaprogramming. We will do this by using
method_missing to delegate identity methods to another object, namely,
meta meta meta
In Ruby there’s only one hard-coded
false object. You can’t
FalseClass.new to make a new, clean one. Even if you could,
false’s singleton class (also known as metaclass) is strangely identical to its regular class:
irb(main):001:0> FalseClass == (class << false; self; end) => true
This is unlike
Symbol, which don’t have singleton classes at all (see here for why).
There’s no way to retrieve regular false functionality after you do this, as far as I know.
even more meta meta
Anyone know why metametaclasses inherit the methods of metaclasses?
irb(main):001:0> require 'rubygems'; require 'metaid' #=> true irb(main):002:0> a = "something" #=> "something" irb(main):003:0> a.metaclass.instance_eval("def ok; true; end") #=> nil irb(main):004:0> a.ok #=> NoMethodError irb(main):005:0> a.class.ok #=> NoMethodError irb(main):006:0> a.metaclass.ok #=> true irb(main):007:0> a.metaclass.metaclass.ok #=> true
That doesn’t seem right. Shouldn’t the last one be
Ahh the joys of Ruby trickery :)
Thank goodness! I was literally just wondering how to do this! ;)