[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Code to DRY

Michel Demazure

10/30/2007 8:31:00 PM

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 ?
--
Posted via http://www.ruby-....

10 Answers

Martin DeMello

10/30/2007 8:59:00 PM

0

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

Konrad Meyer

10/31/2007 12:19:00 AM

0

Quoth 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

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

--
Konrad Meyer <konrad@tylerc.org> http://konrad.sobertil...

Michel Demazure

10/31/2007 7:24:00 AM

0

Martin DeMello wrote:
> How about:
>

> end

Thanks Martin, clean and simple.
--
Posted via http://www.ruby-....

Michel Demazure

10/31/2007 7:25:00 AM

0

Konrad Meyer 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.

--
Posted via http://www.ruby-....

Robert Klemme

10/31/2007 10:33:00 AM

0

2007/10/31, Michel Demazure <michel@demazure.com>:
> Konrad Meyer 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.

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

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

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

Michel Demazure

10/31/2007 10:56:00 AM

0

Robert Klemme wrote:
> 2007/10/31, Michel Demazure <michel@demazure.com>:
>> >> end
>> >> end
>> >
>> > And instead of #le?, why not #<, #==, #>, #<=>?
>>
>> #< looks good. Thanks.
>
> Also keep attention to module Comparable
> http://www.ruby-doc.org/core/classes/Compa...
>
> 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
--
Posted via http://www.ruby-....

Robert Klemme

10/31/2007 12:04:00 PM

0

2007/10/31, Michel Demazure <michel@demazure.com>:
> Robert Klemme wrote:
> > 2007/10/31, Michel Demazure <michel@demazure.com>:
> >> >> end
> >> >> end
> >> >
> >> > And instead of #le?, why not #<, #==, #>, #<=>?
> >>
> >> #< looks good. Thanks.
> >
> > Also keep attention to module Comparable
> > http://www.ruby-doc.org/core/classes/Compa...
> >
> > 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".

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! :-)

Kind regards

robert

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

Michel Demazure

10/31/2007 3:00:00 PM

0

Robert Klemme wrote:
> 2007/10/31, Michel Demazure <michel@demazure.com>:
> 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

--
Posted via http://www.ruby-....

Singlish King is a Wimp

4/19/2011 6:16:00 AM

0

On Apr 19, 12:43 pm, Sotong King <got.eight.l...@hard.to.control>
wrote:
> On 19/04/2011 08:18, Superbee wrote:> On Apr 19, 12:37 am, "truth"<tr...@universe.com>  wrote:
> >> Ha, for GCT to come out to back this Tinny fatt, is a clear indication that
> >> she is bring lots of bad luck to the pap especially Marine Parade. pap
> >> support amongst the younger voters have crashed to less than 5%. The pap is
> >> in deep trouble with the papist clowns running in circles.
>
> > The party is stuck, they cannot pull her out, it will be an admission
>
> ...
>
> but dhey absolutely need young girl like her to mesmerise youngster
> (like me) and young-at-heart lau-chee-ko-peks!!!

Do you really think that anyone pays any attention to what you say,
andjing gila?

You have a really serious mental illness

No matter what ID you use, you are still a dumb ass.

Want to end your mental illness?

It doesn't take much to realize that mental illness itself costs
society an unimaginable amount of money. Let us look at the USA
statistic, it is said that "Among all Americans, 36.2 million people
paid for mental health services totaling $57.5 billion in 2006
<mailbox:///P%7C/ThunderbirdPortable/Data/profile/Mail/Local%20Folders/
Temp­lates?number=30801708&part=1.2&filename=mentalcost2006usa.html>.
" For Canada it is said that "On any given day, 500,000 Canadians are
absent from work due to a form of mental illness – half a million
people
– at a total cost to the Canadian economy of more than $50 billion a
year in lost productivity
<mailbox:///P%7C/ThunderbirdPortable/Data/profile/Mail/Local%20Folders/
Temp­lates?number=30801708&part=1.3&filename=mentalcostcanada.html>.."
Based on USA and Canada, mental illness costs $107.5 billion a YEAR
but
we know that there are other countries who also suffer from similar
financial burden due to the mental illness scam . Based on the fact
that
there are 195 countries
<http://geography.about.com/cs/countries/a/numbercountri... in
the
world,, I will put in is range of trillions annually just to provide
a
figure to the interested audience.

It doesn't take much to realize that mental illness costs
people unnecessary suffering. I have offered to challenge the psych
industry
<mailbox:///P%7C/ThunderbirdPortable/Data/profile/Mail/Local%20Folders/
Temp­lates?number=30801708&part=1.4&filename=index2.html>
and given proof
<mailbox:///P%7C/ThunderbirdPortable/Data/profile/Mail/Local%20Folders/
Temp­lates?number=30801708&part=1.5&filename=selfprot.html>why
mental illness is a scam. As I have stated before, mental illness is
a
created illness. I find it unacceptable to allow people to be
financially burden and having to deal with unnecessary suffering.
Therefore, I will resort to using the public to help me end mental
illness. Why on earth would I take the trouble to do such a thing? I
am
tired of the people in charge of society who would sit and do nothing
while people are made mentally ill. This is why I do it. There are
ways
in which the public can help me amass the resource I need to end
mental
illness. In comparison to what mental illness costs society, the
amount
I ask is insignificant. Given what mental illness costs annually
worldwide, the amount I ask for is meaningless to end the whole
mental
illness scam. Your contribution to the cause of helping me end mental
illness will be documented so that the world will know that you care
about people. As I have stated above, the cost annually worldwide is
trillions but I will just assume a cost of $1 trillion worldwide
annually. Is it too much to ask for 1% of $1 trillion to end mental
illness worldwide which equals $10 billion? Also take into account
that
the mental illness scam has existed for so long that the money that
was
wasted would total to an astronomical amount. There are expense that
has
to be taken in to consideration and people that will be used in the
process in ending the mental illness scam. The mental illness scam is
a
global problem and people all over the world can contribute to the
cause
of ending it.


There are many ways in which the mental illness scam can end but
so
far it has not. If what I do can lead to the mental illness scam
ending
directly or indirectly, then what I do is a service to the world. But
I
don't need $10 billion to end the mental illness scam, $1 billion
will
be sufficient to cover the expense which is a bargain in any
perspective. If I had $1 billion I would have spent it to end the
mental illness scam but I do not so I am counting on the public. This
is
just a step in achieving my goal so any contribution would help
towards
that outcome. There are so many charitable causes in the world that
continue to operate without any resolution but this one is different
because the mental illness scam will end and people will benefit
financially and mentally. Some of you may wonder how come I cannot be
stopped from what I am doing since I do not a medical degree? The
method
which I have outline to rid oneself of mental illness works as long
as
someone does not tamper with what you are doing. The method works
because mental illness is a scam. The government watchdog, drug
companies, researchers in the field of mental health have not end the
mental illness scam so this is the solution which I propose;
financially
support me in my quest to end the mental illness scam.


The contribution that you are making is a step in ending the
mental
illness scam. It can trigger people 's awareness of mental illness
being
a scam. Those agencies which monitored the area of mental health, law
enforcement, etc, who have decided not to take action may take
action.
It can also give you the peace of mind that the end result of your
contribution will result in the ending of the mental illness scam as
it
will be used for that purpose. The mental illness scam continues to
operate because there is nobody like myself who existed that would
take
it this far to challenge the psych industry
<mailbox:///P%7C/ThunderbirdPortable/Data/profile/Mail/Local%20Folders/
Temp­lates?number=30801708&part=1.4&filename=index2.html>.
We want to live life free from being made mentally ill. Your
contribution is to help me help society.



yansimon52

4/19/2011 7:06:00 AM

0

On Apr 19, 8:18 am, Superbee <superb...@gmail.com> wrote:
> On Apr 19, 12:37 am, "truth" <tr...@universe.com> wrote:
>
> > Ha, for GCT to come out to back this Tinny fatt, is a clear indication that
> > she is bring lots of bad luck to the pap especially Marine Parade. pap
> > support amongst the younger voters have crashed to less than 5%. The pap is
> > in deep trouble with the papist clowns running in circles.
>
> The party is stuck, they cannot pull her out, it will be an admission
> of error, but if they keep her in, it will cost them votes. Trapped
> between the devil and the deep blue sea, the next best thing is to put
> her in a strong GRC, in order to minimise the negative impact. The
> first impression is also the last impression, too bad for TPL, the
> first impression wasn't a good one for many. Her choice also make a
> mockery of the limited talent we have in Singapore. Given her public
> persona to date, if that is the best talent we can find, it is only
> logical to conclude that Singapore is doomed. Because, what we are
> asked to believe is that there is no other person in Singapore whose
> is better than TPL. I cannot believe that that is the A-team, not even
> a C-Team, maybe a D-team.

Ayoooh...why the PAP selection team no consideration ah?

How can they mobilise a young woman....haven't really enjoy her full
season of 'orgasm' and they (PAP selection team) want her to be
involved with politics.

Well if they select those old birds like amy Khor or the old old bird
like Yeefoo...then its OK what?...cos, these are already 'old bird'
they already empty their orgasm already what?

PAP selection Team really got no consideration for young ppl leh.