Skip to content

End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

Range#size

rng.size → num

Returns the number of elements in the range. Both the begin and the end of the Range must be Numeric, otherwise nil is returned.

Example
(10..20).size    #=> 11
('a'..'z').size  #=> nil
(-Float::INFINITY..Float::INFINITY).size #=> Infinity
See also #minmax #step #min #to_a also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.1