Code to DRY

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 ?
 
M

Martin DeMello

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
 
K

Konrad Meyer

--nextPart1425879.A7ij99IPBC
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Quoth Martin DeMello:
How about:
=20
module Ordered
def le?(other)
case other
when MAX : true
when MIN : false
else
less_than_or_equal? other
end
end
end
=20
class Klass1
include Ordered
=20
def less_than_or_equal? other
# stuff
end
end

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

=2D-=20
Konrad Meyer <[email protected]> http://konrad.sobertillnoon.com/

--nextPart1425879.A7ij99IPBC
Content-Type: application/pgp-signature; name=signature.asc
Content-Description: This is a digitally signed message part.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQBHJ8nUCHB0oCiR2cwRAn1zAJ9riBQMvWo63Q88T075oT3sDx5zeACdEtUH
W3jett/oaEIm+cT5dIA6Tnc=
=bK62
-----END PGP SIGNATURE-----

--nextPart1425879.A7ij99IPBC--
 
M

Michel Demazure

Robert said:
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,

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
 
R

Robert Klemme

2007/10/31 said:
Robert,

Actually, I have partial orders ("pr=E9ordre" in french) and not orders
("ordre total" in french), so <=3D> does not work well. For instance, you
can have a.le?(b) and b.le?(a), but not a =3D 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 !=3D b".

Ah, I see. I wasn't aware of this. In that case it's probably also
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! :)

Kind regards

robert

--=20
use.inject do |as, often| as.you_can - without end
 
M

Michel Demazure

Robert said:
2007/10/31, Michel Demazure <[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 Doe <= John Doe 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! :)
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top