Skip to content

Maintenance Ruby 3.3 gets fixes, but no new features. 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 #reverse_each #step #overlap? #to_a also in 4.1 · 4.0 · 3.4 · 3.2 · 3.1 · 3.0