Skip to content

Instantly share code, notes, and snippets.

@hynkle
Last active December 12, 2015 12:19
Show Gist options
  • Save hynkle/4771186 to your computer and use it in GitHub Desktop.
Save hynkle/4771186 to your computer and use it in GitHub Desktop.

I'd always assumed that alias_method(new, existing) was equivalent to something like the following (plus support for passing arguments and a block along):

def new
  existing
end

But no, apparently it really does just give you another name with which you can refer to the same method body. Thus:

class Foo

  def foo
    "in foo"
  end
  
  alias_method :bar, :foo

  undef_method :foo

end

Foo.new.bar

Works fine, producing "in foo".

How convenient! I can actually rename a method. (Note: I'm not saying that renaming methods willy-nilly is advisable…)

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