Skip to content

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

instance method

Range#size

size → non_negative_integer or Infinity or nil

Returns the count of elements in self if both begin and end values are numeric; otherwise, returns nil:

Example
(1..4).size      # => 4
(1...4).size     # => 3
(1..).size       # => Infinity
('a'..'z').size  #=> nil

Related: Range#count.

See also #minmax #step #min #to_a also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.0