Create a Wrapper method to alias : alias « Method « Ruby






Create a Wrapper method to alias



class MyClass
  attr_accessor :name, :unit_price

  def initialize(name, unit_price)
    @name, @unit_price = name, unit_price
  end

  def price(quantity=1)
    @unit_price * quantity
  end

  alias :cost :price

  alias :unit_cost :unit_price
  alias :unit_cost= :unit_price=
  def cost(*args)
    price(*args)
  end
end

bacon = MyClass.new("Bacon", 3.95)
bacon.cost(100)                                    # => 399.0

 








Related examples in the same category

1.Give length a new name
2.Alias a old method a new name, then change the implementation of old method
3.Redefining Backquotes
4.Aliasing Methods