Method: Module#redirect_method

Defined in:
lib/nano/module/redirect_method.rb

#redirect_method(method_hash) ⇒ Object

Redirect methods to other methods. This simply defines methods by the name of a hash key which calls the method with the name of the hash’s value.

class Example
  redirect_method :hi => :hello, :hey => :hello
  def hello(name)
    puts "Hello, #{name}."
  end
end

e = Example.new
e.hello("Bob")    #=> "Hello, Bob."
e.hi("Bob")       #=> "Hello, Bob."
e.hey("Bob")      #=> "Hello, Bob."

The above class definition is equivalent to:

class Example
  def hi(*args)
    hello(*args)
  end
  def hey(*args)
    hello(*args)
  end
  def hello
    puts "Hello"
  end
end


33
34
35
36
37
# File 'lib/nano/module/redirect_method.rb', line 33

def redirect_method( method_hash )
  method_hash.each do |targ,adv|
    define_method(targ) { |*args| send(adv,*args) }
  end
end