One of my favorite readable Ruby conventions is appending ?
to the names of methods that return a boolean value and !
to methods that are destructive (i.e. they modify their subject in place rather than returning a modified copy). This Ruby style guide is a good read, if you're looking for more information. However, conventions are only helpul when they're followed... Consider a simplified version of a sticky case I encountered a while ago:
def log(str)
puts str.prepend('logging information: ')
end
# meanwhile, in an important part of your code
x = 'something important'
log(x)
if x == 'something important' then
puts 'do something important'
end
Two hours later, I realized that something important
wasn't happening and that the logging method was to blame — the observer effect in action. Lo, String.prepend
from the standard library is destructive but doesn't end with !
.