On Sun, 2 Jan 2005 nobu.nokada / softhome.net wrote:

> At Mon, 27 Dec 2004 13:02:41 +0900,
> Florian Gro? wrote in [ruby-core:04027]:
> > On the ruby-muse mailing list which is a place for discussion of new 
> > ideas for Ruby there recently was a sub-thread concerning the 
> > introduction of user-defined number literal suffixes. These suffixes are 
> > already used in other languages and look like 1.5f or 0.6r. I think 
> > having these would be nice for user-defined numeric Classes like 
> > Rational (currently these overload 1/2 under mathn and other operations 
> > which can be a problem) and imaginary numbers (via the Complex class). I 
> > think being able to write 0.5r instead of the much longer 
> > Rational.reduce(1, 2), 2i instead of Complex.new(2, 1) or 5.1b instead 
> > of BigDecimal.new("5.1") would be a nice thing that would not have many 
> > downsides.
> As for Rational, almost cannot be represented in that way.

Here are practical ways to achieve similar results using only standard
Ruby.

First, Rational.reduce(a,b) has a shortcut as Rational(a,b) already.
Then you could do:

require "rational"
require "complex"

module Kernel
  alias Q Rational
  alias C Complex
end

p Q(355,113)        #==> Rational(355, 113)
p C(0.5,0.75**0.5)  #==> Complex(0.5, 0.866025403784439)

and then, naturally,

class Rational; def inspect; "Q(#{numerator},#{denominator})" end end
class Complex ; def inspect; "C(#{real     },#{imag       })" end end

so that

p Q(355,113)        #==> Q(355,113)
p C(0.5,0.75**0.5)  #==> C(0.5,0.866025403784439)

and additionally,

I = Complex(0,1)
def I(x) x*I end

so that

p I      #==> C(0,1)
p 2*I    #==> C(0,2)
p I*2    #==> C(0,2)
p I 2    #==> C(0,2)
p I(2)   #==> C(0,2)
p 5+I(2) #==> C(5,2)

which are nice shorthands that are _almost_ as good as changing the
parser.

...

Else you can use techniques of differential psychology by creating an
alias RingOfFractionsOverTheRingOfIntegers Rational, and then feel lucky
to be able to use Rational as a shortcut. ;-)

_____________________________________________________________________
Mathieu Bouchard -=- Montr?al QC Canada -=- http://artengine.ca/matju