Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.ruby
Re: .. and ... aren't operators, maybe they should be?
Eric Mahurin
5/6/2005 9:50:00 PM
I was hoping this would work:
class Range
def Range.new(first,last,exclusive=false)
super(first||-1.0/0,last||+1.0/0,exclusive)
end
end
Range(nil,0) -> -Infinity..0
nil..0 -> ArgumentError: bad value for range
Unfortunately, the .. and ... operators don't call Range.new
directly. There is some checking in between. Otherwise you
could do all of your overrides in Range.new. It just would be
as clean as having .. and ... as methods for overriding class
specific cases.
--- Logan Capaldo <logancapaldo@gmail.com> wrote:
> On 5/5/05, Logan Capaldo <logancapaldo@gmail.com> wrote:
> > I was just wondering if maybe the range construction syntax
> could be
> > implemented as a set of operators?
> >
> > ie
> >
> > class Object
> > def ..(other)
> > Range.new(self, other)
> > end
> > def ...(other)
> > Range.new(self, other, true)
> > end
> > end
> >
> > This wouldn't implement the parser much (I don't think any
> way) and
> > it could be useful for things like
> > defining inifinite lazy lists, or other ranges with weird
> natures
> >
> > ie
> >
> > class InfRange
> > attr_reader :first
> > def take(n)
> > i = self.first
> > 1.upto(n) { yield(i); i = i.succ }
> > end
> > def initialize(n)
> > @first = n
> > end
> > end
> >
> > class Fixnum
> > def ..(other)
> > if other.nil?
> > InfRange.new(self)
> > else
> > Range.new(self, other)
> > end
> > end
> > def ...(other)
> > if other.nil?
> > InfRange.new(self)
> > else
> > Range.new(self, other, true)
> > end
> > end
> > end
> >
> > (1..inf).take(5) do |x|
> > p x
> > end
> >
> > Ok thats a pretty silly example, but lets say succ was more
> > complicated than just self + 1
> >
> > Eh maybe I'm crazy and this is useless. Just seems to be in
> line with
> > << and + etc. being changeable.
> >
>
> Nobody likes this idea? Or is it just so dumb its not worthy
> of a response? ;)
>
>
Discover Yahoo!
Find restaurants, movies, travel and more fun for the weekend. Check it out!
http://discover.yahoo.com/we...
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
Re: .. and ... aren't operators, maybe they should be?
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password