Skip to content

Instantly share code, notes, and snippets.

@bchase
Last active August 29, 2015 14:17
Show Gist options
  • Save bchase/ffb9ae3a7f5658a44627 to your computer and use it in GitHub Desktop.
Save bchase/ffb9ae3a7f5658a44627 to your computer and use it in GitHub Desktop.
module Nottable
class Notter < BasicObject
attr_reader :orig
def initialize(orig)
@orig = orig
end
def equal?(other)
! orig.equal? other
end
def _boolean_methods
orig.methods.select {|m| m =~ /\?$/}
end
def method_missing(method, *args, &block)
return super unless _boolean_methods.include? method
! orig.send method, *args, &block
end
end
def not
Notter.new self
end
end
String.include Nottable
''.nil?
# => false
''.not.nil?
# => true
''.respond_to? :nil?
# => true
''.not.respond_to? :nil?
# => false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment