# Automatic Range?

**URL:** <https://www.ruby-forum.com/t/automatic-range/123289>\
**Category:** Ruby\
**Created:** [December 16, 2007, 1:17am UTC](https://www.ruby-forum.com/t/automatic-range/123289 "2007-12-16T01:17:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jari\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a698b9/32.png) [@Jari\_W](https://www.ruby-forum.com/u/Jari_W)\
**Post date:** [December 16, 2007, 1:17am UTC](https://www.ruby-forum.com/t/automatic-range/123289/1 "2007-12-16T01:17:14Z")

</div>

Problem is simple:  
I want to know if an integer is within the range of x and y. However, I  
don’t automatically know which of x & y is the largest.

Ruby accepts ranges like (5…2), although there seems to be no real use  
for them.

Anyway, is it possible to automatically create a “valid range”, or is  
this kind of solution the best one?

range = x \< y ? (x…y) : (y…x)

Best regards,

Jari W.

---

<div class="post-metadata">

**Author:** ![Jari\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a698b9/32.png) [@Jari\_W](https://www.ruby-forum.com/u/Jari_W)\
**Post date:** [December 16, 2007, 1:30am UTC](https://www.ruby-forum.com/t/automatic-range/123289/2 "2007-12-16T01:30:40Z")

</div>

On Dec 15, 2007, at 7:15 PM, Jari W. wrote:

> range = x \< y ? (x…y) : (y…x)
> 
> Best regards,
> 
> Jari W.

That’s probably as good as it gets. However, Ranges with #end \<  
#begin are common as arguments to String#[] such as “abcdefg”[2…-2]  
=\> “cdef”.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Jari\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a698b9/32.png) [@Jari\_W](https://www.ruby-forum.com/u/Jari_W)\
**Post date:** [December 16, 2007, 9:50am UTC](https://www.ruby-forum.com/t/automatic-range/123289/3 "2007-12-16T09:50:24Z")

</div>

Rob B. wrote:

> However, Ranges with #end \< #begin  
> are common as arguments to String#[] such as “abcdefg”[2…-2] =\> “cdef”.

Ah, of course - there it makes sense. Thanks!

Best regards,

Jari W.

---

<div class="post-metadata">

**Author:** ![Jari\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a698b9/32.png) [@Jari\_W](https://www.ruby-forum.com/u/Jari_W)\
**Post date:** [December 16, 2007, 9:59am UTC](https://www.ruby-forum.com/t/automatic-range/123289/4 "2007-12-16T09:59:10Z")

</div>

On Dec 16, 2007 1:15 AM, Jari W.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> range = x \< y ? (x…y) : (y…x)  
> [x, y].min … [x,y].max  
> is an alternative  
> Robert

–

[http://ruby-smalltalk.blogspot.com/](http://ruby-smalltalk.blogspot.com/)

* * *

All truth passes through three stages. First, it is ridiculed. Second,  
it is violently opposed. Third, it is accepted as being self-evident.  
Schopenhauer (attr.)
