类 Enumerator::Product
Enumerator::Product 生成任意数量可枚举对象的笛卡尔积。迭代可枚举对象的乘积大致等同于嵌套的 each_entry 循环,其中最右侧对象的循环位于最内层。
innings = Enumerator::Product.new(1..9, ['top', 'bottom']) innings.each do |i, h| p [i, h] end # [1, "top"] # [1, "bottom"] # [2, "top"] # [2, "bottom"] # [3, "top"] # [3, "bottom"] # ... # [9, "top"] # [9, "bottom"]
对每个可枚举对象使用的方法是 “each_entry”,而不是 “each”,以便 N 个可枚举对象的乘积在每次迭代中产生一个由 N 个元素组成的数组。
如果没有给定枚举器,它会调用给定的块一次,产生一个空的参数列表。
这种类型的对象可以通过 Enumerator.product 创建。
公共类方法
Enumerator::Product.new(*enums) → enum 单击切换源代码
生成一个新的枚举器对象,该对象生成给定可枚举对象的笛卡尔积。
e = Enumerator::Product.new(1..3, [4, 5]) e.to_a #=> [[1, 4], [1, 5], [2, 4], [2, 5], [3, 4], [3, 5]] e.size #=> 6
static VALUE
enum_product_initialize(int argc, VALUE *argv, VALUE obj)
{
struct enum_product *ptr;
VALUE enums = Qnil, options = Qnil;
rb_scan_args(argc, argv, "*:", &enums, &options);
if (!NIL_P(options) && !RHASH_EMPTY_P(options)) {
rb_exc_raise(rb_keyword_error_new("unknown", rb_hash_keys(options)));
}
rb_check_frozen(obj);
TypedData_Get_Struct(obj, struct enum_product, &enum_product_data_type, ptr);
if (!ptr) rb_raise(rb_eArgError, "unallocated product");
ptr->enums = rb_ary_freeze(enums);
return obj;
}
公共实例方法
each { |...| ... } → obj 单击切换源代码
each → enumerator
通过使用给定的参数调用第一个可枚举对象上的 “each_entry” 方法来迭代其元素,然后按顺序继续处理后续的可枚举对象,直到所有可枚举对象都被耗尽。
如果没有给定块,则返回枚举器。否则,返回自身。
static VALUE
enum_product_each(VALUE obj)
{
RETURN_SIZED_ENUMERATOR(obj, 0, 0, enum_product_enum_size);
return enum_product_run(obj, rb_block_proc());
}
inspect → string 单击切换源代码
返回产品枚举器的可打印版本。
static VALUE
enum_product_inspect(VALUE obj)
{
return rb_exec_recursive(inspect_enum_product, obj, 0);
}
rewind → obj 单击切换源代码
通过按相反的顺序调用每个可枚举对象上的 “rewind” 方法来倒回产品枚举器。只有当可枚举对象响应此方法时,才会执行每次调用。
static VALUE
enum_product_rewind(VALUE obj)
{
struct enum_product *ptr = enum_product_ptr(obj);
VALUE enums = ptr->enums;
long i;
for (i = 0; i < RARRAY_LEN(enums); i++) {
rb_check_funcall(RARRAY_AREF(enums, i), id_rewind, 0, 0);
}
return obj;
}
size → int, Float::INFINITY 或 nil 单击切换源代码
返回通过乘以产品中可枚举对象的大小计算得出的枚举器产品的总大小。如果任何可枚举对象将其大小报告为 nil 或 Float::INFINITY,则该值将作为大小返回。
static VALUE
enum_product_size(VALUE obj)
{
return enum_product_total_size(enum_product_ptr(obj)->enums);
}