module Polynomials class Point include Comparable attr_accessor :x, :y def self.inherited(subclass) self.class_eval do define_method :"#{subclass.name.demodulize.underscore}?" do self.is_a? subclass end end end def initialize(x,y) @x,@y = x,y end def to_s "(#{self.x.inspect},#{self.y.inspect})" end def <=>(other) if self.x < other.x -1 elsif self.x > other.x 1 elsif self.x == other.x && self.y == other.y 0 end end def eql?(other) self.x == other.x && self.y == other.y && other.class == self.class end end class Extremum < Point attr_accessor :kind_of_extremum [:maximum,:minimum].each do |extremum| self.superclass.class_eval do define_method :"#{extremum}?" do self.extremum? && self.kind_of_extremum == extremum end end end def initialize(*args,kind_of_extremum) @kind_of_extremum = kind_of_extremum super(*args) end def to_s super + kind_of_extremum.to_s end def eql?(other) super && self.kind_of_extremum == other.kind_of_extremum end end class Root < Point def initialize(x) super(x,0) end end class InflectionPoint < Point end end