3 ms·
TrueClass and FalseClass aren't exactly "classes" in the usual sense - for instance, `TrueClass.new` throws a NoMethodError. Under the hood, true and false are
by al2o3cr 13y ago
TrueClass and FalseClass aren't exactly "classes" in the usual sense - for instance, `TrueClass.new` throws a NoMethodError.
Under the hood, true and false are singletons - `(1 == 1).object_id` will always be 20. At least in MRI / YARV, that makes booleans much cheaper in terms of memory (a single pointer vs. a pointer + an RObject struct) but means that they don't have their own instance methods.
Keeping the classes separate makes it possible to metaprogram additional behavior onto true and false - for instance, the `blank?` method added by ActiveSupport.
- Argorak 13y agoDon't rely on that ;) Ruby 1.9.3 > (1==1).object_id => 2 > (1!=1).object_id => 0