Ruby/Method/Overloading

Материал из Wiki.crossplatform.ru

(Различия между версиями)
Перейти к: навигация, поиск
м (1 версия: Импорт выборки материалов по Ruby)
 

Текущая версия на 17:55, 13 сентября 2010

Overloading Methods

class Rectangle
  def initialize(*args)
    case args.size
    when 2
      @top_left, @bottom_right = args
    when 3
      @top_left, length, width = args
      @bottom_right = [@top_left[0] + length, @top_left[1] - width]
    else
      raise ArgumentError, "This method takes either 2 or 3 arguments."
    end
  end
end
puts Rectangle.new([10, 23], [14, 13])
puts Rectangle.new([10, 23], 4, 10)
puts Rectangle.new