Skip to content

Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →

instance method

Range#size

() -> ::Integer?
() -> ::Float?
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

If self is not iterable, raises an exception:

Example
(0.5..2.5).size  # TypeError
(..1).size       # TypeError

Related: Range#count.

Ruby version changes

  1. 3.4
See also #reverse_each #step #overlap? #to_a also in 4.1 · 4.0 · 3.3 · 3.2 · 3.1 · 3.0