Skip to content

Instantly share code, notes, and snippets.

@notactuallypagemcconnell
Last active November 16, 2018 22:08
Show Gist options
  • Select an option

  • Save notactuallypagemcconnell/2bb50f62e65377b8d9797f4e31784f86 to your computer and use it in GitHub Desktop.

Select an option

Save notactuallypagemcconnell/2bb50f62e65377b8d9797f4e31784f86 to your computer and use it in GitHub Desktop.
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