I've always enjoyed the ruby convention of using question marks to denote boolean methods. empty?, exist?, any?, alive?, etc... They're concise and easy to read. I do have one gripe with it, sometimes testing the inverse makes my code read like yoda wrote it.
not sting.empty?not path.file?not param.blank?
In rails I use not something.blank? a lot, so creating a not_blank? method wrapper around blank? was a nice simple fix. But I'm lazy and easily give into the temptation of the dark side, so I created a Bizarro Object. Which lets me do this...
# Meaningless examples that prove my point
"".not.empty?#=> false
"words".not.empty?#=> true0.not.zero?#=> false1.not.zero?#=> true
And here is the bit of code that makes it work...
classBizarroObject# Since this is a proxy, get rid of every default method# Copied from Jim Weirich's BlankSlate class# http://onestepback.org/index.cgi/Tech/Ruby/BlankSlate.rdoc
instance_methods.each { |m| undef_method m unless m =~/^__/ }
# Takes an object as a parameter and will invert the return values of all it's methods.definitialize(object)
@object= object
enddefmethod_missing(symbol,*args)
!@object.send(symbol,*args)
endendclassObject# This is where the proxy/bizarro object is createddefnotBizarroObject.new(self)
endend
It's not optimized, and it's a little ridiculous, but I love that ruby lets me abuse it like this.