Project

General

Profile

Actions

Feature #13495

closed

add Range#count as an alias to Range#size

Added by AaronLasseigne (Aaron Lasseigne) about 7 years ago. Updated almost 7 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-core:80831]

Description

For infinite ranges you can't call count, you have to call size.

irb> (1..Float::INFINITY).count # have to interrupt to stop it

irb> (1..Float::INFINITY).size
=> Infinity

The problem with this is that Range is an Enumerable. Enumerable does not have size it has count. So, if you want to implement a method for any Enumerable and you want to check the number of items you can't rely on count. Instead you have to do:

enum_count =
  begin
    size
  rescue NameError
    count
  end

Making Range#count as an alias of Range#size would allow people to make methods for Enumerable classes that rely on count.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0