Skip to content

Instantly share code, notes, and snippets.

@mbj
Created February 25, 2015 01:30
Show Gist options
  • Save mbj/05242e51ce6d80a32f54 to your computer and use it in GitHub Desktop.
Save mbj/05242e51ce6d80a32f54 to your computer and use it in GitHub Desktop.
# original code
class Foo
def foo
:original
end
end
# reference made from original code
method = Foo.new.method(:foo)
# monkey patch
class Foo
def foo
:patched
end
end
# Reference gets evaluated
# Effect not visible. I'm surprised.
p method.call # => :original
@plexus
Copy link

plexus commented Feb 25, 2015

I'm not that surprised. Similar to what alias does

# original code
class Foo
  def foo
    :original
  end
  alias bar foo
end

# monkey patch
class Foo
  def foo
    :patched
  end
end

p Foo.new.bar # => :original

Which is also an interesting caveat, especially when providing "convenience" aliases for a "humane" interface.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment