Skip to content

Instantly share code, notes, and snippets.

@davidcelis
Last active February 27, 2020 22:31
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidcelis/2831543 to your computer and use it in GitHub Desktop.
Save davidcelis/2831543 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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