Object

Object#blank?

By default all objects are blank if they are nil. Individual subclasses define their own blank? methods.

This primarily affects String, Array and Hash objects. All three return true if the object is either nil or empty.

Inspiration

This was originally inspired by Rails, but I've wished for it outside of Rails so I've added it.

Object#in?

Returns a boolean value indicating whether or not object is part of an enumerable object of some sort. The argument to Object#in? must respond to the include? method.

a = ['alpha', 1, 2, 3]

2.in?(a)     # => true
'foo'.in?(a) # => false
3.in?(4)     # => ArgumentError

Object#to_bool

Returns a boolean indicating whether or not the object is nil or false. To be true, an object must not be nil and not be false.

Inspiration

An idiom I see in practice that I don't like is the double negation syntax that some programmers use in order to determine if the object is false or nil:

!!var       # => ick
var.to_bool # => more readable

C extension writers will recognize that this is just an RTEST() call.

Also available in: HTML TXT