Created
April 1, 2025 16:07
-
-
Save robertdfrench/36e072e4c14bd493f5afd5cf9f9b0341 to your computer and use it in GitHub Desktop.
April Fools' 2025: Randomly lie about .nil? to make a ruby program undebuggable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
# A Mean Ruby trick for April Fools | |
# | |
# This is why code review is important. | |
module NilTracer | |
def self.included(base) | |
base.class_eval do | |
alias_method :original_nil?, :nil? | |
def nil? | |
if rand(3) == 0 | |
return (not original_nil?) | |
end | |
original_nil? | |
end | |
end | |
end | |
end | |
Object.include(NilTracer) | |
NilClass.include(NilTracer) | |
puts "5 is never nil" | |
x = 5 | |
10.times { || puts x.nil? } | |
puts "nil is always nil" | |
x = nil | |
10.times { || puts x.nil? } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment