From: Joel VanderWerf Date: 2008-02-15T15:16:11+09:00 Subject: Re: Cute open range? Clifford Heath wrote: > So I wanted to allow folk using my DSL to say "0..N", > "1..N", etc, to describe the cardinality of a relationship, > and I needed to find a useful way to define N without > making it a fixed integer maximum value. > > Well, I came up with this, which I thought was cute: > > class N > def self.coerce(n) > [ n, n+1 ] # Anything you can do, I can do better > end > end > > Now when I ask (0..N) === m, for any m, it says "true", > and when I say the following, it never ends: > > (0..N).each do |n| > puts n > end > > But more importantly, I can say: > > do_something if range.last == N > > Ruby is sweeet :-). Does that do anything that Infinity doesn't do? irb(main):001:0> Infinity = 1/0.0 => Infinity irb(main):002:0> (0..Infinity) === 3 => true irb(main):003:0> (0..Infinity) === -1 => false irb(main):004:0> (0..Infinity).each do |n| irb(main):005:1* p n irb(main):006:1> break if n > 3 irb(main):007:1> end 0 1 2 3 4 => nil irb(main):008:0> puts "do_something" if (0..Infinity).last == Infinity do_something => nil Also, there's this.... class N def self.coerce(n) [ n, n+1 ] end end x = 3 + N p x # ==> 7 w.t.f.? Infinity = 1/0.0 x = 3 + Infinity p x # ==> Infinity -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407