From: ruby-core@... Date: 2017-04-25T05:11:54+00:00 Subject: [ruby-core:80853] [Ruby trunk Feature#13495][Rejected] add Range#count as an alias to Range#size Issue #13495 has been updated by marcandre (Marc-Andre Lafortune). Status changed from Open to Rejected `size` calculates the size lazily, without enumerating. If it can't, it returns `nil`. `count` calculates the size by doing the actual enumeration. It never returns `nil`, but may never finish. (1..10**8).count # => takes a few seconds (1..10**8).size # => instant Note that some ranges don't have sizes (mostly because I was too lazy to implement it for ranges of strings): ('a1'..'zz').size # => nil ('a1'..'zz').count # => 259 In short: understand the difference and use whichever method is right for the task. ---------------------------------------- Feature #13495: add Range#count as an alias to Range#size https://bugs.ruby-lang.org/issues/13495#change-64459 * Author: AaronLasseigne (Aaron Lasseigne) * Status: Rejected * Priority: Normal * Assignee: * Target version: ---------------------------------------- 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`. -- https://bugs.ruby-lang.org/ Unsubscribe: