4 ms·
I don't know Ruby, but this caught my eye: true.class #=> TrueClass false.class #=> FalseClass Why would they have two different classes (types?) for what
by jader201 13y ago
I don't know Ruby, but this caught my eye:
true.class #=> TrueClass
false.class #=> FalseClass
Why would they have two different classes (types?) for what I consider values of a boolean/bit type?
- jonahx 13y agohttp://stackoverflow.com/questions/3192978/why-does-ruby-have-trueclass-and-falseclass-instead-of-a-single-boolean-class http://stackoverflow.com/questions/3192978/why-does-ruby-hav...
- al2o3cr 13y agoTrueClass 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