为什么Enumerable在Ruby中没有length属性?

时间:2021-09-09 12:23:23

At least in Ruby 1.9.3, Enumerable objects do not have a length attribute.

至少在Ruby 1.9.3中,Enumerable对象没有length属性。

As far as I can tell, anything Enumerable is a set, as evidenced by methods like sort and find_index.

据我所知,任何Enumerable都是一个集合,如sort和find_index等方法所证明。

A set always has a well-defined length (...right?), so why is this not a property?

一个集总是有一个定义明确的长度(......对吗?),为什么这不是一个属性?

3 个解决方案

#1


10  

Enumerable has the count method, which is usually going to be the intuitive "length" of the enumeration.

Enumerable有count方法,它通常是枚举的直观“长度”。

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.

但为什么不把它称为“长度”?好吧,因为它的运作方式非常不同。在Ruby的内置数据结构(如Array和Hash)中,length只检索数据结构的预先计算大小。它应该总是立即返回。

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.

但是,对于Enumerable #count,它无法知道它正在运行什么类型的结构,因此没有快速,巧妙的方法来获取枚举的大小(这是因为Enumerable是一个模块,并且可以包含在任何模块中类)。获取枚举大小的唯一方法是实际枚举它并按其计算。对于无限枚举,count将(适当地)永远循环并且永不返回。

#2


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:

枚举不能保证有长度 - 对于Enumerable混合的对象的唯一要求是它响应#each,导致它返回系列中的下一个项目,#<=>允许比较提供的值由可数。像#sort这样的方法将在排序过程中枚举整个集合,但可能不会提前知道集合的边界。考虑:

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!

在迭代器生成值“500”之前,将调用此枚举,这将导致它停止枚举。收集并排序结果集。但是,在这种情况下,#length方法没有意义,因为在迭代器耗尽之前,长度是不可知的!

We can call #length on the result of things like #sort, since they return an array, though:

我们可以在#sort之类的结果上调用#length,因为它们返回一个数组,但是:

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.

通常,当长度已知并且可以在恒定时间内返回时使用#length,而当提前知道长度并且需要通过迭代计算时,倾向于使用#count(有时是#size)。结果集(因此,采用线性时间)。如果您需要Enumerable提供的结果集的大小,请尝试使用.to_a.length #count。

#3


0  

Enumerable isn't really a class, it's a module - a collection of cross-cutting functionality that is used by multiple classes.

Enumerable并不是真正的类,它是一个模块 - 由多个类使用的交叉功能的集合。

For example, Array, Set and Hash all include it - you can call any of the Enumerable methods on them.

例如,Array,Set和Hash都包含它 - 您可以调用它们上的任何Enumerable方法。

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:

可枚举值得注意的是它只需要很少的“主机”类。您需要做的就是定义每个方法并包含Enumerable,并且您可以免费获得所有这些方法!例:

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.

正如你所看到的,我从未定义过CountUntil #map,但是我从包含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.

要解决有关长度的问题:并非所有包含Enumerable的类都定义了长度,即使大多数类都有。例如,Enumerator可用于创建无限流。

#1


10  

Enumerable has the count method, which is usually going to be the intuitive "length" of the enumeration.

Enumerable有count方法,它通常是枚举的直观“长度”。

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.

但为什么不把它称为“长度”?好吧,因为它的运作方式非常不同。在Ruby的内置数据结构(如Array和Hash)中,length只检索数据结构的预先计算大小。它应该总是立即返回。

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.

但是,对于Enumerable #count,它无法知道它正在运行什么类型的结构,因此没有快速,巧妙的方法来获取枚举的大小(这是因为Enumerable是一个模块,并且可以包含在任何模块中类)。获取枚举大小的唯一方法是实际枚举它并按其计算。对于无限枚举,count将(适当地)永远循环并且永不返回。

#2


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:

枚举不能保证有长度 - 对于Enumerable混合的对象的唯一要求是它响应#each,导致它返回系列中的下一个项目,#<=>允许比较提供的值由可数。像#sort这样的方法将在排序过程中枚举整个集合,但可能不会提前知道集合的边界。考虑:

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!

在迭代器生成值“500”之前,将调用此枚举,这将导致它停止枚举。收集并排序结果集。但是,在这种情况下,#length方法没有意义,因为在迭代器耗尽之前,长度是不可知的!

We can call #length on the result of things like #sort, since they return an array, though:

我们可以在#sort之类的结果上调用#length,因为它们返回一个数组,但是:

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.

通常,当长度已知并且可以在恒定时间内返回时使用#length,而当提前知道长度并且需要通过迭代计算时,倾向于使用#count(有时是#size)。结果集(因此,采用线性时间)。如果您需要Enumerable提供的结果集的大小,请尝试使用.to_a.length #count。

#3


0  

Enumerable isn't really a class, it's a module - a collection of cross-cutting functionality that is used by multiple classes.

Enumerable并不是真正的类,它是一个模块 - 由多个类使用的交叉功能的集合。

For example, Array, Set and Hash all include it - you can call any of the Enumerable methods on them.

例如,Array,Set和Hash都包含它 - 您可以调用它们上的任何Enumerable方法。

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:

可枚举值得注意的是它只需要很少的“主机”类。您需要做的就是定义每个方法并包含Enumerable,并且您可以免费获得所有这些方法!例:

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.

正如你所看到的,我从未定义过CountUntil #map,但是我从包含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.

要解决有关长度的问题:并非所有包含Enumerable的类都定义了长度,即使大多数类都有。例如,Enumerator可用于创建无限流。