M
Michel Demazure
This is a simplified version of the problem I have. I work with
"ordered" classes, i.e. having a method a.le?(b) (le = lower or equal).
But I want two constants MIN and MAX, same for all classes, with
MIN.le?(a) == true, a.le?(MAX) == true, a.le?(MIN) only if a == MIN and
MAX.le?(a) only if a == MAX. Moreover, I want a default a.le?(b) when
le? is not defined for a.Class.
This is a possibility
class MaxClass
def le?(other); other == self ; end
end
class MinClass
def le?(other); true ; end
end
MAX = MaxClass.new
MIN = MinClass.new
def method_missing(name, *args)
if name == "le?"
case args[0]
when MAX : true
when MIN : false
else
# real thing for default
end
else
super
end
end
class Klass1
def le?(other)
case other
when MAX : true
when MIN : false
else
# real thing for Klass1
end
end
end
class Klass2
def le?(other)
case other
when MAX : true
when MIN : false
else
# real thing for Klass1
end
end
end
and so on
Obviously not DRY !
Suggestions ?
"ordered" classes, i.e. having a method a.le?(b) (le = lower or equal).
But I want two constants MIN and MAX, same for all classes, with
MIN.le?(a) == true, a.le?(MAX) == true, a.le?(MIN) only if a == MIN and
MAX.le?(a) only if a == MAX. Moreover, I want a default a.le?(b) when
le? is not defined for a.Class.
This is a possibility
class MaxClass
def le?(other); other == self ; end
end
class MinClass
def le?(other); true ; end
end
MAX = MaxClass.new
MIN = MinClass.new
def method_missing(name, *args)
if name == "le?"
case args[0]
when MAX : true
when MIN : false
else
# real thing for default
end
else
super
end
end
class Klass1
def le?(other)
case other
when MAX : true
when MIN : false
else
# real thing for Klass1
end
end
end
class Klass2
def le?(other)
case other
when MAX : true
when MIN : false
else
# real thing for Klass1
end
end
end
and so on
Obviously not DRY !
Suggestions ?