# Range max

**URL:** <https://rubytalk.org/t/range-max/41327>\
**Category:** ruby-talk\
**Created:** [12 October 2007 01:30 UTC](https://rubytalk.org/t/range-max/41327 "2007-10-12T01:30:23Z")\
**Posts on this page:** 11\
**Page:** 2

<div class="post-metadata">

**Author:** ![Brian\_Adkins](https://avatars.discourse-cdn.com/v4/letter/b/3ab097/32.png) [@Brian\_Adkins](https://rubytalk.org/u/Brian_Adkins)\
**Post date:** [13 October 2007 02:45 UTC](https://rubytalk.org/t/range-max/41327/21 "2007-10-13T02:45:02Z")

</div>

Actually, I think the following would be better for those rare people  
who find themselves frequently wondering what the largest int in a  
range is, and would prefer O(1) instead of O(n):

class Range  
&nbsp;&nbsp;alias orig\_max max  
&nbsp;&nbsp;def max  
&nbsp;&nbsp;&nbsp;&nbsp;if ( self.begin.kind\_of?(Integer) &&  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.end.kind\_of?(Integer) &&  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.begin \<= self.end )  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.exclude\_end? ? self.end - 1 : self.end  
&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;orig\_max  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

Now if someone can just come up with a real world example (i.e.  
currently existing code in production) of Ruby code that actually  
requires this, I'll be amazed. 🙂

> **···**
>
> On Oct 12, 9:59 pm, Brian Adkins \<lojicdot...@gmail.com\> wrote:
> 
> > On Oct 12, 1:25 pm, "ara.t.howard" \<ara.t.how...@gmail.com\> wrote:
> > 
> > \> On Oct 12, 2007, at 9:40 AM, Brian Adkins wrote:  
> > \> \> Actually, what you want is simply N. It's a nonsensical request from a  
> > \> \> troll. Using max, end, last, etc. to find the end of the range is  
> > \> \> ridiculous since you must know the end of the range to define the  
> > \> \> range!
> > 
> > \> that is not true. any object can be used in a range in ruby. it  
> > \> only must respond to #succ. it's quite possible to declare classes  
> > \> that worked like so
> > 
> > I'm not unfamiliar with the domain of the Range class. Let's not get  
> > ridiculous here; I was referring specifically to integer ranges.  
> > Nothing unambiguates like code, eh? 🙂
> > 
> > class Range  
> > &nbsp;&nbsp;def max\_int  
> > &nbsp;&nbsp;&nbsp;&nbsp;raise 'invalid range' if self.end \< self.begin  
> > &nbsp;&nbsp;&nbsp;&nbsp;self.end - (self.exclude\_end? ? 1 : 0)  
> > &nbsp;&nbsp;end  
> > end

---

<div class="post-metadata">

**Author:** ![Brian\_Adkins](https://avatars.discourse-cdn.com/v4/letter/b/3ab097/32.png) [@Brian\_Adkins](https://rubytalk.org/u/Brian_Adkins)\
**Post date:** [13 October 2007 02:05 UTC](https://rubytalk.org/t/range-max/41327/22 "2007-10-13T02:05:02Z")

</div>

> \> With respect to an integer range, a combination of Range#end and  
> \> Range#exclude\_end?
> 
> 0 ... 1
> 
> we know that max != 0, so

Wrong. (0...1).max\_int -\> 0 (see code in other post)

> max = 0.9?  
> max = 0.99?  
> max = 0.999?  
> max = 0.9999?

What the heck are you talking about? Do you really think that 0.9 or  
0.99 is in the range (0...1) ?

> **···**
>
> On Oct 12, 9:52 pm, "ara.t.howard" \<ara.t.how...@gmail.com\> wrote:
> 
> > On Oct 12, 2007, at 7:45 PM, Brian Adkins wrote:

---

<div class="post-metadata">

**Author:** ![Brian\_Adkins](https://avatars.discourse-cdn.com/v4/letter/b/3ab097/32.png) [@Brian\_Adkins](https://rubytalk.org/u/Brian_Adkins)\
**Post date:** [13 October 2007 03:05 UTC](https://rubytalk.org/t/range-max/41327/23 "2007-10-13T03:05:03Z")

</div>

D'oh! Old code failed for (0...0)

class Range  
&nbsp;&nbsp;alias orig\_max max  
&nbsp;&nbsp;def max  
&nbsp;&nbsp;&nbsp;&nbsp;if ( self.begin.kind\_of?(Integer) &&  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.end.kind\_of?(Integer) &&  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;( self.exclude\_end? ?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.begin \< self.end :  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.begin \<= self.end ) )  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.exclude\_end? ? self.end - 1 : self.end  
&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;orig\_max  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

---

<div class="post-metadata">

**Author:** ![a11](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/a11/32/8169_2.png) [@a11](https://rubytalk.org/u/a11)\
**Post date:** [13 October 2007 02:42 UTC](https://rubytalk.org/t/range-max/41327/24 "2007-10-13T02:42:58Z")

</div>

> > 0 ... 1
> > 
> > we know that max != 0, so
> 
> Wrong. (0...1).max\_int -\> 0 (see code in other post)

you code does not return the max, it returns the 'end', and incorrectly in some cases.

any range/interval whose end \> start \*must\* have size \> 0. therefore the max cannot == start. i think you are confusing the concept of sequence with that of a range

&nbsp;&nbsp;&nbsp;[Sequence - Wikipedia](http://en.wikipedia.org/wiki/Sequence) # sequence  
&nbsp;&nbsp;&nbsp;[Interval (mathematics) - Wikipedia](http://en.wikipedia.org/wiki/Interval_%28mathematics%29) # range

there is \*no\* constraint on ruby ranges to be either - one may use the range to represent either. if one uses it to represent an interval the, with an open ended (... vs ..) range there is no way to compute the max. this is a very useful property because you can use it to do things like

&nbsp;&nbsp;f = 0.42

&nbsp;&nbsp;r = 0 ... 1

&nbsp;&nbsp;if f \> r.start and f \< r.end

&nbsp;&nbsp;&nbsp;&nbsp;...

so, if we are talking general purpose methods, which the OP was, Range#max cannot be written to be general purpose, even for integer 'ranges'

> > max = 0.9?  
> > max = 0.99?  
> > max = 0.999?  
> > max = 0.9999?
> 
> What the heck are you talking about? Do you really think that 0.9 or  
> 0.99 is in the range (0...1) ?

yes. but it is not in the integer sequence.

regards.

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

> **···**
>
> On Oct 12, 2007, at 8:05 PM, Brian Adkins wrote:  
> --  
> share your knowledge. it's a way to achieve immortality.  
> h.h. the 14th dalai lama

---

<div class="post-metadata">

**Author:** ![a11](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/a11/32/8169_2.png) [@a11](https://rubytalk.org/u/a11)\
**Post date:** [13 October 2007 04:02 UTC](https://rubytalk.org/t/range-max/41327/25 "2007-10-13T04:02:14Z")

</div>

p ( -1 .. -4 ).max

😉

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

> **···**
>
> On Oct 12, 2007, at 9:05 PM, Brian Adkins wrote:
> 
> > D'oh! Old code failed for (0...0)
> > 
> > class Range  
> > &nbsp;&nbsp;alias orig\_max max  
> > &nbsp;&nbsp;def max  
> > &nbsp;&nbsp;&nbsp;&nbsp;if ( self.begin.kind\_of?(Integer) &&  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.end.kind\_of?(Integer) &&  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;( self.exclude\_end? ?  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.begin \< self.end :  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.begin \<= self.end ) )  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.exclude\_end? ? self.end - 1 : self.end  
> > &nbsp;&nbsp;&nbsp;&nbsp;else  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;orig\_max  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;end  
> > end
> 
> --  
> share your knowledge. it's a way to achieve immortality.  
> h.h. the 14th dalai lama

---

<div class="post-metadata">

**Author:** ![Brian\_Adkins](https://avatars.discourse-cdn.com/v4/letter/b/3ab097/32.png) [@Brian\_Adkins](https://rubytalk.org/u/Brian_Adkins)\
**Post date:** [13 October 2007 03:25 UTC](https://rubytalk.org/t/range-max/41327/26 "2007-10-13T03:25:02Z")

</div>

> you code does not return the max, it returns the 'end', and  
> incorrectly in some cases.

Fixed now 🙂

> any range/interval whose end \> start \*must\* have size \> 0. therefore  
> the max cannot == start. i think you are confusing the concept of  
> sequence with that of a range

1) Range#max is mixed in from Enumerable  
2) "When used as an iterator, ranges return each value in the  
\*sequence\*." Pickaxe p. 597  
3) An elephant's trunk feels different than its tail  
4) "range max conversation".max == this post

> **···**
>
> On Oct 12, 10:42 pm, "ara.t.howard" \<ara.t.how...@gmail.com\> wrote:
> 
> > On Oct 12, 2007, at 8:05 PM, Brian Adkins wrote:

---

<div class="post-metadata">

**Author:** ![Brian\_Adkins](https://avatars.discourse-cdn.com/v4/letter/b/3ab097/32.png) [@Brian\_Adkins](https://rubytalk.org/u/Brian_Adkins)\
**Post date:** [13 October 2007 04:50 UTC](https://rubytalk.org/t/range-max/41327/27 "2007-10-13T04:50:02Z")

</div>

Are you complaining about my version of max or the built in? I can't  
tell because the results are identical 😉

> **···**
>
> On Oct 13, 12:02 am, "ara.t.howard" \<ara.t.how...@gmail.com\> wrote:
> 
> > On Oct 12, 2007, at 9:05 PM, Brian Adkins wrote:
> > 
> > \> D'oh! Old code failed for (0...0)
> > 
> > \> class Range  
> > \> alias orig\_max max  
> > \> def max  
> > \> if ( self.begin.kind\_of?(Integer) &&  
> > \> self.end.kind\_of?(Integer) &&  
> > \> ( self.exclude\_end? ?  
> > \> self.begin \< self.end :  
> > \> self.begin \<= self.end ) )  
> > \> self.exclude\_end? ? self.end - 1 : self.end  
> > \> else  
> > \> orig\_max  
> > \> end  
> > \> end  
> > \> end
> > 
> > p ( -1 .. -4 ).max
> > 
> > 😉

---

<div class="post-metadata">

**Author:** ![Luu\_Trung](https://avatars.discourse-cdn.com/v4/letter/l/85f322/32.png) [@Luu\_Trung](https://rubytalk.org/u/Luu_Trung)\
**Post date:** [13 October 2007 03:29 UTC](https://rubytalk.org/t/range-max/41327/28 "2007-10-13T03:29:53Z")

</div>

Brian Adkins wrote:

> **···**
>
> > On Oct 12, 10:42 pm, "ara.t.howard" \<ara.t.how...@gmail.com\> wrote:
> > 
> > > On Oct 12, 2007, at 8:05 PM, Brian Adkins wrote:  
> > > you code does not return the max, it returns the 'end', and  
> > > incorrectly in some cases.
> > 
> > Fixed now 🙂
> > 
> > > any range/interval whose end \> start \*must\* have size \> 0. therefore  
> > > the max cannot == start. i think you are confusing the concept of  
> > > sequence with that of a range
> > 
> > 1) Range#max is mixed in from Enumerable  
> > 2) "When used as an iterator, ranges return each value in the  
> > \*sequence\*." Pickaxe p. 597  
> > 3) An elephant's trunk feels different than its tail  
> > 4) "range max conversation".max == this post
> 
> File Not Found  
> June 08, 2006
> 
> The file you were looking for could not be found.
> 
> Attempted URL: /faq/rubyfaq.html
> 
> It is possible that you typed the URL incorrectly or that you clicked on  
> a bad link.  
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![a11](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/a11/32/8169_2.png) [@a11](https://rubytalk.org/u/a11)\
**Post date:** [13 October 2007 04:04 UTC](https://rubytalk.org/t/range-max/41327/29 "2007-10-13T04:04:46Z")

</div>

> 1) Range#max is mixed in from Enumerable

but #include? is not

> 2) "When used as an iterator, ranges return each value in the  
> \*sequence\*." Pickaxe p. 597

that is true, and here it is an interval

case 0.5  
&nbsp;&nbsp;&nbsp;when 0 ... 1  
&nbsp;&nbsp;&nbsp;&nbsp;p true  
&nbsp;&nbsp;&nbsp;when 1 ... 2  
&nbsp;&nbsp;&nbsp;&nbsp;p false  
end

they can act as either

> 4) "range max conversation".max == this post

the OP's question is about why the current max is slow. the reason is because ranges are not always sequences.

regards.

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

> **···**
>
> On Oct 12, 2007, at 9:25 PM, Brian Adkins wrote:  
> --  
> it is not enough to be compassionate. you must act.  
> h.h. the 14th dalai lama

---

<div class="post-metadata">

**Author:** ![a11](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/a11/32/8169_2.png) [@a11](https://rubytalk.org/u/a11)\
**Post date:** [13 October 2007 23:34 UTC](https://rubytalk.org/t/range-max/41327/30 "2007-10-13T23:34:27Z")

</div>

not complaining about anything. over the years people have posted various questions and complaints about ranges: they should do this and that, etc. most/all of the posts though are based on a misconception of what ranges are: a very lightweight set of endpoints with minimal constraints on the contents and, of course, this is what makes then so generally useful in ruby.

basically i feel that the ruby range is maligned and i'm making sure the limitations don't convince people that they aren't powerful. the OP, for those who haven't lost track, had complained about the performance of range.max. the thing for people still on thread to remember is simply that the impl is naive because ranges are so general and that a faster implementation would limit the usefulness of range itself, as this thread as beat to death.

cheers.

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

> **···**
>
> On Oct 12, 2007, at 10:50 PM, Brian Adkins wrote:
> 
> > Are you complaining about my version of max or the built in? I can't  
> > tell because the results are identical 😉
> 
> --  
> we can deny everything, except that we have the possibility of being better. simply reflect on that.  
> h.h. the 14th dalai lama

---

<div class="post-metadata">

**Author:** ![Brian\_Adkins](https://avatars.discourse-cdn.com/v4/letter/b/3ab097/32.png) [@Brian\_Adkins](https://rubytalk.org/u/Brian_Adkins)\
**Post date:** [13 October 2007 04:45 UTC](https://rubytalk.org/t/range-max/41327/31 "2007-10-13T04:45:07Z")

</div>

Dude, you totally missed your cue about point #3. You were supposed to  
say, "no, you're wrong, an elephant's trunk feels the same as its  
tail". Cool, I guess we do agree on something 🙂

> **···**
>
> On Oct 13, 12:04 am, "ara.t.howard" \<ara.t.how...@gmail.com\> wrote:
> 
> > On Oct 12, 2007, at 9:25 PM, Brian Adkins wrote:
> > 
> > \> 1) Range#max is mixed in from Enumerable
> > 
> > but #include? is not
> > 
> > \> 2) "When used as an iterator, ranges return each value in the  
> > \> \*sequence\*." Pickaxe p. 597
> > 
> > that is true, and here it is an interval
> > 
> > case 0.5  
> > &nbsp;&nbsp;&nbsp;when 0 ... 1  
> > &nbsp;&nbsp;&nbsp;&nbsp;p true  
> > &nbsp;&nbsp;&nbsp;when 1 ... 2  
> > &nbsp;&nbsp;&nbsp;&nbsp;p false  
> > end
> > 
> > they can act as either
> > 
> > \> 4) "range max conversation".max == this post
> > 
> > the OP's question is about why the current max is slow. the reason  
> > is because ranges are not always sequences.
> > 
> > regards.

[Previous page](https://rubytalk.org/t/range-max/41327.md?page=1)
