Rectangle 27 1

ruby Should I use alias or alias_method?


alias
alias :new_method_name :old_method_name
alias_method
alias_method :new_method_name, :old_method_name
def foo
  "foo"
end

alias_method :baz, :foo

The word he's looking for here is more expected results. alias_method is determined at runtime and not when the code is read, like alias, so it behaves more how we would expect it to.

What do you mean by unpredictable. Navely, one would say that the option that is less flexible will be more predictable. Also, can you provide any practical example of benefitting from redefining alias_method?

alias_method can be redefined if need be. (it's defined in the Module class.)

expecting that methods are defined on the fly during runtime is not what most programmers expect. At least it's like flying pigs to me.

Note
Rectangle 27 1

ruby Should I use alias or alias_method?


# scoping with alias_method
class User

  def full_name
    puts "Johnnie Walker"
  end

  def self.add_rename
    alias_method :name, :full_name
  end

end

class Developer < User
  def full_name
    puts "Geeky geek"
  end
  add_rename
end

Developer.new.name #=> 'Gekky geek'
class User

  def full_name
    puts "Johnnie Walker"
  end

  def self.add_rename
    alias :name :full_name
  end
end

class Developer < User
  def full_name
    puts "Geeky geek"
  end
  add_rename
end

Developer.new.name #=> 'Johnnie Walker'

the main difference is in the scoping

In the above case method name picks the method full_name defined in Developer class. Now lets try with alias.

This is because alias is a keyword and it is lexically scoped. It means it treats self as the value of self at the time the source code was read . In contrast alias_method treats self as the value determined at the run time.

With the usage of alias the method name is not able to pick the method full_name defined in Developer.

Note
Rectangle 27 1

ruby Should I use alias or alias_method?


class Engine
  def start
    #code goes here
  end
  alias run start
end
class Engine
  def start
    puts "start me"
  end
end

Engine.new.start() # => start me

Engine.class_eval do
  unless method_defined?(:run)
    alias_method :run, :start
    define_method(:start) do
      puts "'before' extension"
      run()
      puts "'after' extension"
    end
  end
end

Engine.new.start
# => 'before' extension
# => start me
# => 'after' extension

Engine.new.run # => start me

I think there is an unwritten rule (something like a convention) that says to use 'alias' just for registering a method-name alias, means if you like to give the user of your code one method with more than one name:

Note
Rectangle 27 1

ruby Should I use alias or alias_method?


A point in favor of alias instead of alias_method is that its semantic is recognized by rdoc, leading to neat cross references in the generated documentation, while rdoc completely ignore alias_method.

How is RDoc supposed to understand the consequences of a method that is evaluated at runtime?

Maybe RDoc should start treating alias_method the same as alias. We should tell them about it ;)

Totally agree. Yard handles this pretty well if I remember correctly...

Note