# Re: A left-exclusive Range class

**URL:** <https://www.ruby-forum.com/t/re-a-left-exclusive-range-class/101096>\
**Category:** Ruby\
**Created:** [August 3, 2006, 12:41pm UTC](https://www.ruby-forum.com/t/re-a-left-exclusive-range-class/101096 "2006-08-03T12:41:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 3, 2006, 12:41pm UTC](https://www.ruby-forum.com/t/re-a-left-exclusive-range-class/101096/1 "2006-08-03T12:41:11Z")

</div>

> The current Range class in ruby is quite useful, however, I was recently  
> trying to capture a simple algorithm and ended up needing to express  
> left-exclusion.

Can you tell me what that algorithm was ? It may be blindness, but  
at the moment, I do not understand in what circumstances one might  
need such a feature, whereas applications for right-exclusion are  
obvious  
to me …

Thanks.

Best regards,

Axel

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 3, 2006, 12:42pm UTC](https://www.ruby-forum.com/t/re-a-left-exclusive-range-class/101096/2 "2006-08-03T12:42:03Z")

</div>

On Wed, 2006-08-02 at 23:24 +0900, removed\_email\_address@domain.invalid wrote:

> > The current Range class in ruby is quite useful, however, I was recently  
> > trying to capture a simple algorithm and ended up needing to express  
> > left-exclusion.
> 
> Can you tell me what that algorithm was ? It may be blindness, but  
> at the moment, I do not understand in what circumstances one might  
> need such a feature, whereas applications for right-exclusion are obvious  
> to me …

Sure thing. I’m working on a simulation environment for various  
Distribute Hash Table (DHT) algorithms. It is common for such  
algorithms to define the key-space a node is responsible for as

(n, n.successor]

While it is often okay to just use [n, n.successor] (which is what I’m  
currently doing), it would be nice to feel the implementation followed  
from theory.

I don’t mean to make this sound like a critical issue, just a general  
query. 🙂

ciao,  
Cyrus

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 3, 2006, 12:52pm UTC](https://www.ruby-forum.com/t/re-a-left-exclusive-range-class/101096/3 "2006-08-03T12:52:47Z")

</div>

Cyrus Hall wrote:

> Sure thing. I’m working on a simulation environment for various  
> query. 🙂  
> The simplest thing you can always do is to create a method that (and  
> optionally a special range class if you like) implements this as syntax  
> modifications are always difficult. If you do

module Kernel  
private

def lor®  
r.exclude\_end? ? r.first.succ … r.last : r.first.succ … r.last  
end  
end

You can do

irb(main):030:0\> for i in lor 1…10 do puts i end  
2  
3  
4  
5  
6  
7  
8  
9  
10  
=\> 2…10  
irb(main):031:0\> for i in lor 1…10 do puts i end  
2  
3  
4  
5  
6  
7  
8  
9  
=\> 2…10  
irb(main):032:0\>

Kind regards

```
robert
```

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 3, 2006, 12:54pm UTC](https://www.ruby-forum.com/t/re-a-left-exclusive-range-class/101096/4 "2006-08-03T12:54:25Z")

</div>

On Thu, 2006-08-03 at 01:35 +0900, Robert K. wrote:

> end  
> This isn’t quite right however, as lor(0…10).include?(0.5) would  
> return false (when it should return true). However, what you suggest is  
> what I’m doing right now, as every DHT I’ve ever seen uses whole  
> numbers. Never the less, looking for a better solution…

I’m writing the unit tests for a little replacement class right now.  
Once they all pass, I’ll post it for others.

Cyrus

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 3, 2006, 2:24pm UTC](https://www.ruby-forum.com/t/re-a-left-exclusive-range-class/101096/5 "2006-08-03T14:24:47Z")

</div>

Cyrus Hall wrote:

> > end  
> > end
> 
> This isn’t quite right however, as lor(0…10).include?(0.5) would  
> return false (when it should return true). However, what you suggest is  
> what I’m doing right now, as every DHT I’ve ever seen uses whole  
> numbers. Never the less, looking for a better solution…

Yeah, that was just a quick sample hack to illustrate the idea.

> I’m writing the unit tests for a little replacement class right now.  
> Once they all pass, I’ll post it for others.

As I see you got the hang of it already. 🙂

Cheers

```
robert
```

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 3, 2006, 12:56pm UTC](https://www.ruby-forum.com/t/re-a-left-exclusive-range-class/101096/6 "2006-08-03T12:56:37Z")

</div>

Facets has an Interval class.

T.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 3, 2006, 2:33pm UTC](https://www.ruby-forum.com/t/re-a-left-exclusive-range-class/101096/7 "2006-08-03T14:33:49Z")

</div>

On Thu, 2006-08-03 at 02:00 +0900, removed\_email\_address@domain.invalid wrote:

> Facets has an Interval class.
> 
> T.

Thanks transfire. This is exactly what I was looking for. I’ve got my  
own up and running now, but the Facets library looks very useful, and a  
good place to check for useful classes in the future.

Cyrus
