3

I was working with Ruby and WIN32OLE (which is specifically, Excel).

I found all those enumerable objects, such as Range.Columns, are automatically enumerable in Ruby, and can be iterated using obj.each method.

I am wondering how it works? I understand that if you want to make something enumerable, you have to include "Enumerable". But apparently they cannot put that line in the OLE object. Is it just directly mapping obj.each method to for each loop?

Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
SwiftMango
  • 15,092
  • 13
  • 71
  • 136
  • Check the source code. They probably inherit from a base object that includes Enumerable. – theglauber Mar 29 '12 at 22:09
  • You may want to try `obj.class.ancestors` to see what class the object is, and what ancestors it has. – Andrew Grimm Mar 29 '12 at 22:14
  • The ancestors of `Columns` collection are: [WIN32OLE, Object, Kernel, BasicObject]. It is the same for `Excel.Application`. But `Columns` is enumerable while `Excel.Application` cannot. – SwiftMango Mar 30 '12 at 12:45

1 Answers1

0

There's an interesting set of posts here that might answer your question, specifically:

(...) each is called as dynamically as any other OLE method; it's not determined before the call whether or not the object actually implements IEnum.

and:

Enumerable#find method clashes the 'find' method of Excel Range object. This is the (only) reason why WIN32OLE does not include Enumerable.

WIN32OLE class has 'each' method (WIN32OLE#each is defined) (...)

Hope this helps!

Nathan Kleyn
  • 5,103
  • 3
  • 32
  • 49
  • What about calling `.to_enum` on a WIN32OLE object to get access to all of the `Enumerable` goodies without risking the collisions? – tovodeverett Mar 03 '14 at 20:54