You cannot achieve this.
The return value of s.nil?
is either true
or false
, which are the only instances of TrueClass
and FalseClass
, respectively. You cannot create other instances of the two classes. So you cannot monkey patch TrueClass
or FalseClass
to remember the context s
. You can define singleton method or
on true
and false
, however, it knows nothing about s
so you cannot chain another predicate empty?
.
def true.or other
true
end
def false.or other
other
end
You can write s.nil?.or(s.empty?)
with the two helper method defined.
Another thought is to return a customized object for all predicate (blabla?) methods, rather than true
or false
. However, in ruby, only nil
and false
yield false, any other stuffs yield true. You instance of the customized object will always be true, which will break all those predicate methods.