Ruby max integer
Ruby automatically converts integers to a large integer class when they overflow, so there's (practically) no limit to how big they can be.
If you are looking for the machine's size, i.e. 64- or 32-bit, I found this trick at ruby-forum.com:
machine_bytes = ['foo'].pack('p').size
machine_bits = machine_bytes * 8
machine_max_signed = 2**(machine_bits-1) - 1
machine_max_unsigned = 2**machine_bits - 1
If you are looking for the size of Fixnum objects (integers small enough to store in a single machine word), you can call 0.size
to get the number of bytes. I would guess it should be 4 on 32-bit builds, but I can't test that right now. Also, the largest Fixnum is apparently 2**30 - 1
(or 2**62 - 1
), because one bit is used to mark it as an integer instead of an object reference.
Ruby get maximum integer size value
There's no maximum any more for integers, they automatically shift to "bignum" representation:
1 << 64
# => 18446744073709551616
(1 << 64) + 1
# => 18446744073709551617
There's really no limit other than memory:
1 << (1 << 16)
# => 20035299304...(thousands of digits)...05719156736
As for Time, it's similarly unbounded so now you can express times well after the heat death of the universe if you really want to:
Time.at(1<<128)
# => 10783118943836478994022445751222-08-06 04:04:16 -0400
This used to be limited to the usual +/- 2.1 billion range, subject to the 2038 problem but that hasn't been the case since Ruby ~1.9. I'm not sure where 2116 factors in except from a Windows perspective.
If you want to know the max/min that can be represented in a "native" integer then that's platform dependent. 32-bit and 64-bit binaries will have different limits.
Max number for ActiveModel::Type::Integer
It's a signed 4 byte integer.
So the maximum value it can have is 2³¹ - 1
2147483647
How to find a min/max with Ruby
You can do
[5, 10].min
or
[4, 7].max
They come from the Enumerable module, so anything that includes Enumerable
will have those methods available.
v2.4 introduces own Array#min
and Array#max
, which are way faster than Enumerable's methods because they skip calling #each
.
@nicholasklick mentions another option, Enumerable#minmax
, but this time returning an array of [min, max]
.
[4, 5, 7, 10].minmax
=> [4, 10]
Related Topics
How to Use Bundler Behind a Proxy
Ruby on Rails - Access Controller Variable from Model
Haml: Append Class If Condition Is True in Haml
Ruby 1.9.2 How to Install Rmagick on Windows
"Certificate Verify Failed" Openssl Error When Using Ruby 1.9.3
How to Avoid Trailing Empty Items Being Removed When Splitting Strings
Can't Install Rails on MAC Os Catalina
Is There a Hook Similar to Class#Inherited That's Triggered Only After a Ruby Class Definition
Creating Draft via Google Gmail API
Ruby on Rails Console Is Hanging When Loading
Can You Ask Ruby to Treat Warnings as Errors
One-Liner to Recursively List Directories in Ruby
Getaddrinfo: Nodename Nor Servname Provided, or Not Known
Returning All Maximum or Minimum Values That Can Be Multiple
Error: Mime-Types-Data Requires Ruby Version >= 2.0
Ruby: Parsing a String Representation of Nested Arrays into an Array