Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 627385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:31:21+00:00 2026-05-13T19:31:21+00:00

Ok, so say you have a really big Range in ruby. I want to

  • 0

Ok, so say you have a really big Range in ruby. I want to find a way to get the max value in the Range.

The Range is exclusive (defined with three dots) meaning that it does not include the end object in it’s results. It could be made up of Integer, String, Time, or really any object that responds to #<=> and #succ. (which are the only requirements for the start/end object in Range)

Here’s an example of an exclusive range:

  past  = Time.local(2010, 1, 1, 0, 0, 0)
  now   = Time.now
  range = past...now

  range.include?(now)  # => false

Now I know I could just do something like this to get the max value:

  range.max  # => returns 1 second before "now" using Enumerable#max

But this will take a non-trivial amount of time to execute. I also know that I could subtract 1 second from whatever the end object is is. However, the object may be something other than Time, and it may not even support #-. I would prefer to find an efficient general solution, but I am willing to combine special case code with a fallback to a general solution (more on that later).

As mentioned above using Range#last won’t work either, because it’s an exclusive range and does not include the last value in it’s results.

The fastest approach I could think of was this:

  max = nil
  range.each { |value| max = value }

  # max now contains nil if the range is empty, or the max value

This is similar to what Enumerable#max does (which Range inherits), except that it exploits the fact that each value is going to be greater than the previous, so we can skip using #<=> to compare the each value with the previous (the way Range#max does) saving a tiny bit of time.

The other approach I was thinking about was to have special case code for common ruby types like Integer, String, Time, Date, DateTime, and then use the above code as a fallback. It’d be a bit ugly, but probably much more efficient when those object types are encountered because I could use subtraction from Range#last to get the max value without any iterating.

Can anyone think of a more efficient/faster approach than this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T19:31:22+00:00Added an answer on May 13, 2026 at 7:31 pm

    The simplest solution that I can think of, which will work for inclusive as well as exclusive ranges:

    range.max
    

    Some other possible solutions:

    range.entries.last
    range.entries[-1]
    

    These solutions are all O(n), and will be very slow for large ranges. The problem in principle is that range values in Ruby are enumerated using the succ method iteratively on all values, starting at the beginning. The elements do not have to implement a method to return the previous value (i.e. pred).

    The fastest method would be to find the predecessor of the last item (an O(1) solution):

    range.exclude_end? ? range.last.pred : range.last
    

    This works only for ranges that have elements which implement pred. Later versions of Ruby implement pred for integers. You have to add the method yourself if it does not exist (essentially equivalent to special case code you suggested, but slightly simpler to implement).

    Some quick benchmarking shows that this last method is the fastest by many orders of magnitude for large ranges (in this case range = 1...1000000), because it is O(1):

                                              user     system      total        real
    r.entries.last                       11.760000   0.880000  12.640000 ( 12.963178)
    r.entries[-1]                        11.650000   0.800000  12.450000 ( 12.627440)
    last = nil; r.each { |v| last = v }  20.750000   0.020000  20.770000 ( 20.910416)
    r.max                                17.590000   0.010000  17.600000 ( 17.633006)
    r.exclude_end? ? r.last.pred : r.last 0.000000   0.000000   0.000000 (  0.000062)
    

    Benchmark code is here.

    In the comments it is suggested to use range.last - (range.exclude_end? ? 1 : 0). It does work for dates without additional methods, but will never work for non-numeric ranges. String#- does not exist and makes no sense with integer arguments. String#pred, however, can be implented.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have three different 2x2 submatrices, and I want to create a big
Let's say I have a really large file foo.txt and I want to iterate
Lets say I have a really big table filled with lots of data (say,
first i want to say that this site has been a really big help
I have a quick question. So, say I have a really big number up
I have had really big problems understand the char* lately. Let's say I made
Let's say we have two objects. Furthermore, let's assume that they really have no
Let's say I have a program to write text into a file (not really
Say I have a Telerik MVC Grid, AJAX bound and I want to put
I have a select SQL query which is really big and it should be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.