[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: nil question

e

1/31/2005 3:44:00 AM

> Lähettäjä: "William James" <w_a_x_man@yahoo.com>
> Aihe: Re: nil question
>
> Austin Ziegler wrote
> > you can also do:
> >
> > puts "yes" if x.between?(-5, 9)
> > puts "yes" if (-5..9).include?(x)
>
> But that's not as readable as -5 < x < 9, which is how it is written
> for humans in algebra.
>
> In Icon, comparisons succeed or fail, they don't return true or false.
> So it works this way (if x==0):
> (-5 < 0 ) succeeds and produces 0
> (0 < 9 ) succeeds and produces 9
>
> 1 < x < 9
> (1 < 0) fails
>
> -5 < x < -15
> (-5 < 0 ) succeeds and produces 0
> (0 < -15 ) fails
>
> So in Icon, a < x < b means exactly what it does in algebra.
>
> Matz and many Ruby gurus think that Ruby incorporates the best
> features of preceding languages (e.g., Awk, Perl, Python, Smalltalk).
> But how many of these people ever used Icon?

Put this in a file and require it and you'll get the behaviour you
want. Normal comparisons still work, too.

# Create the new comparison methods
module MultiComparable
def <(val)
return val if self.<=>(val) == -1
false
end
def >(val)
return val if self.<=>(val) == 1
false
end
end

# Modify false to allow comparisons (always fail)
class FalseClass
def <(val)
false
end
def >(val)
false
end
end

# Redefine all numeric comparisons
class Fixnum; remove_method :<; remove_method :>; include MultiComparable; end
class Bignum; remove_method :<; remove_method :>; include MultiComparable; end
class Float; remove_method :<; remove_method :>; include MultiComparable; end
class Complex; remove_method :<; remove_method :>; include MultiComparable; end

I actually tested it now :)

E