1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

What is the Difference Between alias and #alias_method?

By

What is the Difference Between alias and #alias_method?

Ruby has this mysterious thing called alias. It'll take one thing and make it have a second name. In other words, it'll take one method and give it a second name. It'll create an alias for something under a second name.

But there is a bit of strangeness here. It is implemented in an un-Ruby way, it is implemented as a keyword. Not many things are implemented as keywords in Ruby, so this is a bit strange. In addition, it takes two arguments that need not be escaped or made into symbols in any way, nor do they need to be separated with commas. So, for example, you could say alias begin start to make the #begin method the same as the #start method from within a class. Those two words are not variable names or method calls, they're naked words, like keywords. This is something that is very rarely seen in Ruby.

This does look utterly bizarre. You never see naked words like that, they're not strings or symbols, they're just... words. And as I said before, there's no comma. But, thankfully, you can at least make this look a bit more Ruby-like by using symbols, the alias method seems to like symbols just fine. So you could say alias :begin :start and it'll at least play a little more nicely with your editor's syntax highlighting. And, as of Ruby 2.0.0 (which has been feature locked), you can even use the comma, so it can now be indistinguishable from a method.

But, other than the syntax, there's plenty of reasons not to use the alias keyword. First off, the scoping rules are a bit funky. If I were to "call" the alias keyword from a class method, it operates on the class that method is defined in, rather than the class it was called from. This is completely counterintuitive. Second, because of the funky syntax you can't take a few values out of an array variable or from method parameters and give them to alias. If you reference a variable in an alias statement, it never gets "interpreted." Since the keyword accepts naked words, there's no way to tell whether you're referring to the variable methname, or to the method called methname that you're wanting to alias. The alias keyword doesn't take variables in any way.

To illustrate this point, here is an example.


#!/usr/bin/env ruby

class Parent
  def self.do_alias
    alias hello greet
  end

  def self.do_alias_method
    alias_method :hello, :greet
  end

  def greet
    puts "Hello from Parent class!"
  end
end

class Child1 < Parent
  def greet
    puts "Hello from Child1 class!"
  end

  do_alias
end

class Child2 < Parent
  def greet
    puts "Hello from Child2 class!"
  end

  do_alias_method
end

Child1.new.hello
Child2.new.hello

The Parent class has two methods that perform aliases. The do_alias method uses the alias keyword. It will make a "new" method called hello, which is the same method as greet. Since you're calling this method from a child class, you'd expect it to use the method from the child class, but this is not how alias works. Alias has a very narrow view of the world, and only looks at the methods in the class it was called from. So the Child1 class that calls do_alias will print "Hello from Parent class!" when the hello method is called on an instance of that class.

The Child2 method illustrates why the alias_method method is preferred to the alias keyword. This acts as you'd expect it to. Since you called do_alias_method from the child class, this is the greet method that gets referred to.

So why use any of these at all? The most common use is a monkey patch. You can use alias to "rename" a method to a temporary name, then define a new method under the same name. This will preserve the old method, perhaps to be called by the new method, while still having your new method to redefine behavior. At any rate, this is a bit of strangeness that you need to be aware of, even if you never find yourself using the alias method.

  1. About.com
  2. Technology
  3. Ruby
  4. Question and Answer
  5. What is the Difference Between alias and #alias_method?

©2014 About.com. All rights reserved.