# A left-exclusive Range class

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

<div class="post-metadata">

**Author:** ![Cyrus\_Hall](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c77e96/32.png) [@Cyrus\_Hall](https://www.ruby-forum.com/u/Cyrus_Hall)\
**Post date:** [August 3, 2006, 12:39pm UTC](https://www.ruby-forum.com/t/a-left-exclusive-range-class/101093/1 "2006-08-03T12:39:45Z")

</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. This is the one feature ruby currently does not support  
in its Range class.

I did some digging into 1.8.4’s Range implementation and it appears  
that the changes needed to support left-exclusion would be minimal.  
Rather, the problem seems syntactical. Currently ‘…’ and ‘…’ are  
used to represent right-inclusion and -exclusion, respectively. This  
syntax clearly can not be extended to left-exclusion. There appears to  
have been a related debate about this on ruby-talk back in Oct. 2004,  
with the conclusion being, to paraphrase the entire discussion, “this is  
hard, lets forget about it for now.”

Has this been discussed since 2004? Would people be interested in  
finding a way to get left-exclusion into the Range class? This may be  
something that has been addressed in all the language work going into  
1.9/2.0, and if so, any pointers would be greatly appreciated.

Ciao,  
Cyrus Hall

---

<div class="post-metadata">

**Author:** ![Cyrus\_Hall](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c77e96/32.png) [@Cyrus\_Hall](https://www.ruby-forum.com/u/Cyrus_Hall)\
**Post date:** [August 3, 2006, 4:58pm UTC](https://www.ruby-forum.com/t/a-left-exclusive-range-class/101093/2 "2006-08-03T16:58:18Z")

</div>

Cyrus Hall wrote:

> have been a related debate about this on ruby-talk back in Oct. 2004,  
> with the conclusion being, to paraphrase the entire discussion, “this is  
> hard, lets forget about it for now.”
> 
> Has this been discussed since 2004? Would people be interested in  
> finding a way to get left-exclusion into the Range class? This may be  
> something that has been addressed in all the language work going into  
> 1.9/2.0, and if so, any pointers would be greatly appreciated.
> 
> Ciao,  
> Cyrus Hall

You could try just appropriating a different piece of syntax and using  
it for your own evil purposes, e.g. \<\<

class Fixnum  
def \<\<(number)  
(self + 1)…number  
end  
end

for i in 1\<\<10

# i starts at 2

end

Brian Guthrie

---

<div class="post-metadata">

**Author:** ![Cyrus\_Hall](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c77e96/32.png) [@Cyrus\_Hall](https://www.ruby-forum.com/u/Cyrus_Hall)\
**Post date:** [August 3, 2006, 5:01pm UTC](https://www.ruby-forum.com/t/a-left-exclusive-range-class/101093/3 "2006-08-03T17:01:18Z")

</div>

Brian Guthrie wrote:

> > syntax clearly can not be extended to left-exclusion. There appears to
> 
> # i starts at 2
> 
> end
> 
> Brian Guthrie

Of course by doing that you’ve totally just broken the existing  
Fixnum#\<\<. Bummer, should’ve checked first. Ah, well.

Brian Guthrie

---

<div class="post-metadata">

**Author:** ![Cyrus\_Hall](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c77e96/32.png) [@Cyrus\_Hall](https://www.ruby-forum.com/u/Cyrus_Hall)\
**Post date:** [August 3, 2006, 5:21pm UTC](https://www.ruby-forum.com/t/a-left-exclusive-range-class/101093/4 "2006-08-03T17:21:09Z")

</div>

Brian Guthrie wrote:

> Cyrus Hall wrote:
> 
> > have been a related debate about this on ruby-talk back in Oct. 2004,  
> > with the conclusion being, to paraphrase the entire discussion, “this is  
> > hard, lets forget about it for now.”
> > 
> > Has this been discussed since 2004? Would people be interested in  
> > finding a way to get left-exclusion into the Range class? This may be  
> > something that has been addressed in all the language work going into  
> > 1.9/2.0, and if so, any pointers would be greatly appreciated.

Doesn’t preserve the fact that it started as a left exclusive range, but  
how about:

class Range  
alias :orig\_initialize :initialize  
def initialize( s, e, exclusive=false )  
case exclusive  
when false, true  
orig\_initialize( s, e, exclusive )  
when :left  
orig\_initialize( s.succ, e )  
when :right  
orig\_initialize( s, e, true )  
when :both  
orig\_initialize( s.succ, e, true )  
end  
end  
end

Then you can do Range.new( 1, 5, :left ) or Range.new( 1, 5, :right ) or  
use the … / … operators as well.
