Last active
November 16, 2018 22:08
-
-
Save notactuallypagemcconnell/2bb50f62e65377b8d9797f4e31784f86 to your computer and use it in GitHub Desktop.
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
| require 'fiddle' | |
| require 'pry' | |
| User = Struct.new :name | |
| class NullUser < NilClass | |
| def self.new(*args, &block) | |
| obj = Object.new | |
| obj_ptr = Fiddle::Pointer.new 2*obj.object_id | |
| klass_ptr = Fiddle::Pointer.new 2*self.object_id | |
| obj_ptr[8, 8] = klass_ptr.ref[0, 8] | |
| obj | |
| end | |
| def name | |
| '' | |
| end | |
| end | |
| user = User.new "Bob" # => #<struct User name="Bob"> | |
| puts user.name # => "Bob" | |
| puts user.to_h # => {:name=>"Bob"} | |
| puts user = NullUser.new # => nil | |
| puts user.name # => "" | |
| puts user.to_h # => {} | |
| if user | |
| puts "User exists" | |
| else | |
| puts "User aint real" | |
| end | |
| # => "User exists" | |
| # But user _is nil_ lol |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment