问题
At least in Ruby 1.9.3, Enumerable objects do not have a length
attribute.
As far as I can tell, anything Enumerable
is a set, as evidenced by methods like sort
and find_index
.
A set always has a well-defined length (...right?), so why is this not a property?
回答1:
Enumerable has the count method, which is usually going to be the intuitive "length" of the enumeration.
But why not call it "length"? Well, because it operates very differently. In Ruby's built-in data structures like Array
and Hash
, length
simply retrieves the pre-computed size of the data structure. It should always return instantly.
For Enumerable#count
, however, there's no way for it to know what sort of structure it's operating on and thus no quick, clever way to get the size of the enumeration (this is because Enumerable
is a module, and can be included in any class). The only way for it to get the size of the enumeration is to actually enumerate through it and count as it goes. For infinite enumerations, count
will (appropriately) loop forever and never return.
回答2:
Enumerables are not guaranteed to have lengths - the only requirement for an object which Enumerable is mixed into is that it responds to #each
, which causes it to return the next item in the series, and #<=>
which allows comparison of values provided by the enumerable. Methods like #sort
will enumerate the entire collection over the course of sorting, but may not know the bounds of the set ahead of time. Consider:
class RandomSizeEnumerable
include Enumerable
def each
value = rand 1000
while value != 500
yield value
value = rand 1000
end
end
# Not needed for this example, but included as a part of the Enumerable "interface".
# You only need this method if #max, #min, or #sort are used on this class.
def <=>(a, b)
a <=> b
end
end
This enumerable will be called until the iterator generates the value "500", which will cause it to stop enumerating. The result set is collected and sorted. However, a #length
method is meaningless in this context, because the length is unknowable until the iterator has been exhausted!
We can call #length
on the result of things like #sort
, since they return an array, though:
p RandomSizeEnumerable.new.sort.length # 321
p RandomSizeEnumerable.new.sort.length # 227
p RandomSizeEnumerable.new.sort.length # 299
Conventionally, #length
is used when the length is known and can be returned in constant time, whereas #count
(and sometimes #size
) tend to be used when the length may not be known ahead of time and needs to be computed by iterating the result set (thus, taking linear time). If you need the size of the result set provided by an Enumerable, try using .to_a.length
#count
.
回答3:
Enumerable
isn't really a class, it's a module - a collection of cross-cutting functionality that is used by multiple classes.
For example, Array
, Set
and Hash
all include
it - you can call any of the Enumerable
methods on them.
Enumerable
is notable in that it requires very little of the "host" class. All you need to do is define the each
method and include Enumerable
, and you get all those methods for free! Example:
class CountUntil
def initialize(number)
@number = number
end
include Enumerable
def each
current = 0
while current < @number
yield current
current += 1
end
end
end
# Usage:
CountUntil.new(10).map { |n| n * 5 }
# => [0, 5, 10, 15, 20, 25, 30, 35, 40, 45]
As you can see, I never defined CountUntil#map
, but I got that for free from including Enumerable
.
To address your question about length
: not all classes that include Enumerable
have defined length, even though most do. For example, Enumerator
can be used to create infinite streams.
来源:https://stackoverflow.com/questions/28839037/why-does-enumerable-not-have-a-length-attribute-in-ruby