Skip to content

Instantly share code, notes, and snippets.

@tessi
Last active December 19, 2015 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tessi/5957491 to your computer and use it in GitHub Desktop.
Save tessi/5957491 to your computer and use it in GitHub Desktop.
Observe Hash#[]=
class SnakeHash < SimpleDelegator
def []=(arg1, arg2)
puts "!!1!"
@delegate_sd_obj[arg1] = arg2
end
end
#[2] pry(main)> h = SnakeHash.new(Hash.new)
#=> {}
#[3] pry(main)> h[2]
#=> nil
#[4] pry(main)> h[2] = :foo
#!!1!
#=> :foo
class FakeHash < Hash
def []=(arg1,arg2)
puts "!!!1!"
super
end
end
#[6] pry(main)> i = FakeHash.new
#=> {}
#[7] pry(main)> i[1]
#=> nil
#[8] pry(main)> i[1] = 3
#!!!1!
#=> 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment