back

by jader201·12y ago·view on hn ↗
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?
2 comments
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.

Don't rely on that ;)

Ruby 1.9.3

> (1==1).object_id => 2

> (1!=1).object_id => 0