Skip to content

Instantly share code, notes, and snippets.

@davidcelis
Last active Feb 27, 2020
Embed
What would you like to do?
NotClass
class Object
def not
NotClass.new(self)
end
end
class NotClass < BasicObject
instance_methods.grep(/^[^_]/).each { |m| undef_method m }
def initialize(object)
@object = object
end
def method_missing(name, *args, &block)
!@object.send(name, *args, &block)
end
end
puts nil.not.nil?
puts 1.not.eql?(2)
puts [1, 3, 5, 7, 9].not.any? { |i| i % 2 == 0 }
@davidcelis

This comment has been minimized.

Copy link
Owner Author

@davidcelis davidcelis commented May 30, 2012

require 'benchmark'

Benchmark.bm do |x|
  x.report { 1.upto(1_000_000) do; 1.not == 2; end }
  x.report { 1.upto(1_000_000) do; 1 != 2; end }
end
       user     system      total        real
   0.302335   0.001475   0.303810 (  0.303833)
   0.035334   0.000051   0.035385 (  0.035446)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment