I see that there's a relatively new feature in Ruby which allows chained iteration -- in other words, instead of `each_with_indices { |x,i,j| ... }` you might do `each.with_indices { |x,i,j| ... }`, where `#each` returns an `Enumerator` object, and `Enumerator#with_indices` causes the additional yield parameters to be included.

So, `Enumerator` has its own method `#with_index`, presumably for one-dimensional objects, [source found here](http://ruby-doc.org/core-2.0/Enumerator.html#method-i-with_index). But I can't figure out the best way to adapt this to other objects.

**To be clear, and in response to comments:** Ruby doesn't have an `#each_with_indices` right now -- it's only got an `#each_with_index`. (That's why I want to create one.)

A series of questions, themselves chained:

1. How would one adapt chained iteration to a *one*-dimensional object? Simply do an `include Enumerable`?
2. Presumably the above (#1) would not work for an *n*-dimensional object. Would one create an `EnumerableN` class, derived from `Enumerable`, but with `#with_index` converted into `#with_indices`?
3. Can #2 be done for Ruby extensions written in C? For example, I have a matrix class which stores various types of data (floats, doubles, integers, sometimes regular Ruby objects, *etc.*). Enumeration needs to check the data type (`dtype`) first as per the example below.

Example:

```
VALUE nm_dense_each(VALUE nm) {
  volatile VALUE nm = nmatrix; // Not sure this actually does anything.
  DENSE_STORAGE* s = NM_STORAGE_DENSE(nm); // get the storage pointer

  RETURN_ENUMERATOR(nm, 0, 0);

  if (NM_DTYPE(nm) == nm::RUBYOBJ) { // matrix stores VALUEs

    // matrix of Ruby objects -- yield those objects directly
    for (size_t i = 0; i < nm_storage_count_max_elements(s); ++i)
      rb_yield( reinterpret_cast<VALUE*>(s->elements)[i] );

  } else { // matrix stores non-Ruby data (int, float, etc)

    // We're going to copy the matrix element into a Ruby VALUE and then operate on it. This way user can't accidentally
    // modify it and cause a seg fault.
    for (size_t i = 0; i < nm_storage_count_max_elements(s); ++i) {
      // rubyobj_from_cval() converts any type of data into a VALUE using macros such as INT2FIX()
      VALUE v = rubyobj_from_cval((char*)(s->elements) + i*DTYPE_SIZES[NM_DTYPE(nm)], NM_DTYPE(nm)).rval;
      rb_yield( v ); // yield to the copy we made
    }
  }
}

```

**So, to combine my three questions into one:** How would I write, in C, a `#with_indices` to chain onto the `NMatrix#each` method above?

I don't particularly want anyone to feel like I'm asking them to code this for me, though if you did want to, we'd love to have you involved in our project. =)

But if you know of some example elsewhere on the web of how this is done, that'd be perfect -- or if you could just explain in words, that'd be lovely too.