Code to DRY

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 ?

How about:

module Ordered
def le?(other)
case other
when MAX : true
when MIN : false
else
less_than_or_equal? other
end
end
end

class Klass1
include Ordered

def less_than_or_equal? other
# stuff
end
end

Quoth Martin DeMello:

end
end

class Klass1
include Ordered

def less_than_or_equal? other
# stuff
end
end

And instead of #le?, why not #<, #==, #>, #<=>?

Martin DeMello wrote:

How about:

end

Thanks Martin, clean and simple.

Konrad M. wrote:

Quoth Martin DeMello:

end
end

class Klass1
include Ordered

def less_than_or_equal? other
# stuff
end
end

And instead of #le?, why not #<, #==, #>, #<=>?

#< looks good. Thanks.

2007/10/31, Michel D. [email protected]:

end
end

And instead of #le?, why not #<, #==, #>, #<=>?

#< looks good. Thanks.

Also keep attention to module Comparable
http://www.ruby-doc.org/core/classes/Comparable.html

Basically you need to only implement <=> and get all other operators for
free.

Btw, what do you need min and max for?

Kind regards

robert

Robert K. wrote:

2007/10/31, Michel D. [email protected]:

end
end

And instead of #le?, why not #<, #==, #>, #<=>?

#< looks good. Thanks.

Also keep attention to module Comparable
module Comparable - RDoc Documentation

Basically you need to only implement <=> and get all other operators for
free.

Btw, what do you need min and max for?

Kind regards

robert

Robert,

Actually, I have partial orders (“préordre” in french) and not orders
(“ordre total” in french), so <=> does not work well. For instance, you
can have a.le?(b) and b.le?(a), but not a = b. Or neither a.le?(b), nor
b.le?(a). For instance a.lt?(b) is “a.le?(b) and not b.le?(a)” but not
“a.le?(B) and a != b”.

I need min and max for semantic reasons, analogous to Scott’s semantics
for programming languages : ‘min’ is something like ‘undefined’
(definitely less than ‘defined with value nil’ for instance) and ‘max’
is something like ‘overdefined’ or ‘impossible’. All this for some try
at a “fuzzy knowledge” management program…which actually works quite
well!

Best,
Michel

2007/10/31, Michel D. [email protected]:

module Comparable - RDoc Documentation
Robert,

Actually, I have partial orders (“préordre” in french) and not orders
(“ordre total” in french), so <=> does not work well. For instance, you
can have a.le?(b) and b.le?(a), but not a = b. Or neither a.le?(b), nor
b.le?(a). For instance a.lt?(b) is “a.le?(b) and not b.le?(a)” but not
“a.le?(B) and a != b”.

Ah, I see. I wasn’t aware of this. In that case it’s probably also
not a good idea to use <, > etc. to avoid confusion. Do you use some
kind of topological sort then?

I need min and max for semantic reasons, analogous to Scott’s semantics
for programming languages : ‘min’ is something like ‘undefined’
(definitely less than ‘defined with value nil’ for instance) and ‘max’
is something like ‘overdefined’ or ‘impossible’. All this for some try
at a “fuzzy knowledge” management program…which actually works quite
well!

I still do not know how you use min and max but it certainly sounds
interesting! :slight_smile:

Kind regards

robert

Robert K. wrote:

2007/10/31, Michel D. [email protected]:
Ah, I see. I wasn’t aware of this. In that case it’s probably also
not a good idea to use <, > etc. to avoid confusion. Do you use some
kind of topological sort then?

No, I only use it to find, if any, a “piece of knowledge” b with
a.le?(b).
Toy examples :
“birthday” <= “birthday John in March” <= “birthday John 15th March”…
So, for Persons : MIN <= John <= John D. <= John D. phone=12345 <=…
for Dates “MIN <= March” <= “15th March” <= 15th March 9pm …

I still do not know how you use min and max but it certainly sounds
interesting! :slight_smile:
See above for MIN. MAX happens for instance when you freeze a knowledge
and then modify it. Or when you know too much : “birthday 15th March and
2d April”…

All the best,

Michel