# Cute open range?

**URL:** <https://www.ruby-forum.com/t/cute-open-range/129187>\
**Category:** Ruby\
**Created:** [February 15, 2008, 7:00am UTC](https://www.ruby-forum.com/t/cute-open-range/129187 "2008-02-15T07:00:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Clifford\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/49beb7/32.png) [@Clifford\_H](https://www.ruby-forum.com/u/Clifford_H)\
**Post date:** [February 15, 2008, 7:00am UTC](https://www.ruby-forum.com/t/cute-open-range/129187/1 "2008-02-15T07:00:25Z")

</div>

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

Clifford H…

---

<div class="post-metadata">

**Author:** ![Clifford\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/49beb7/32.png) [@Clifford\_H](https://www.ruby-forum.com/u/Clifford_H)\
**Post date:** [February 15, 2008, 7:16am UTC](https://www.ruby-forum.com/t/cute-open-range/129187/2 "2008-02-15T07:16:49Z")

</div>

Clifford H. wrote:

> end
> 
> 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

---

<div class="post-metadata">

**Author:** ![Clifford\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/49beb7/32.png) [@Clifford\_H](https://www.ruby-forum.com/u/Clifford_H)\
**Post date:** [February 15, 2008, 7:40am UTC](https://www.ruby-forum.com/t/cute-open-range/129187/3 "2008-02-15T07:40:54Z")

</div>

Joel VanderWerf wrote:

> Does that do anything that Infinity doesn’t do?

Hey, I forgot about Infinity, much better idea,  
and you get -Infinity too :-). But this does do  
one thing that Infinity doesn’t: it works correctly  
with Bignums that are too big to be coerced to a  
float. I don’t think that should concern me though :-).

Is there a standard place where Infinity is defined,  
or should I risk re-defining it? Or worse, test and  
define only if needed…

Clifford H…

---

<div class="post-metadata">

**Author:** ![Clifford\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/49beb7/32.png) [@Clifford\_H](https://www.ruby-forum.com/u/Clifford_H)\
**Post date:** [February 15, 2008, 10:51pm UTC](https://www.ruby-forum.com/t/cute-open-range/129187/4 "2008-02-15T22:51:09Z")

</div>

On 15.02.2008 07:34, Clifford H. wrote:

> or should I risk re-defining it? Or worse, test and  
> define only if needed…

Why define or redefine? Why not just

irb(main):001:0\> N = 1/0.0  
=\> Infinity  
irb(main):002:0\> (1…N) === 2  
=\> true  
irb(main):003:0\> (1…N) === -2  
=\> false

etc? I believe this is what Joel wanted to suggest.

Kind regards

robert
