# Enumerable and WIN32OLE

**URL:** <https://www.ruby-forum.com/t/enumerable-and-win32ole/70127>\
**Category:** Ruby\
**Created:** [September 26, 2006, 11:52pm UTC](https://www.ruby-forum.com/t/enumerable-and-win32ole/70127 "2006-09-26T23:52:09Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 26, 2006, 11:52pm UTC](https://www.ruby-forum.com/t/enumerable-and-win32ole/70127/1 "2006-09-26T23:52:09Z")

</div>

I make a lot of use of the WIN32OLE library, with many thanks to Nobu  
Nakada.

One of my common idioms is to take a WIN32OLE object that implements  
the IEnumerable interface and extend it with the Ruby Enumerable Mix  
in. Thus:

```
  rows = @ole_worksheet.usedRange.rows.extend Enumerable
  rows.select do |row|
    entire_row = row.entireRow
    entire_row.columns(1).text == '' and # comment
    entire_row.columns(2).text != '' # no action
  end

```

It strikes me that it might be reasonable to expect the Ruby win32ole  
library to do this for me automatically. Do you agree?

Bret

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 27, 2006, 10:22am UTC](https://www.ruby-forum.com/t/enumerable-and-win32ole/70127/2 "2006-09-27T10:22:24Z")

</div>

removed\_email\_address@domain.invalid wrote:

> One of my common idioms is to take a WIN32OLE object that implements  
> the IEnumerable interface and extend it with the Ruby Enumerable Mix  
> in. Thus:  
> …  
> It strikes me that it might be reasonable to expect the Ruby win32ole  
> library to do this for me automatically. Do you agree?

Yes, I’ve expected that on occasion, and I think it would be a nifty  
feature indeed. Enumerable’s methods don’t clash with common COM  
collection methods, as far as I can see.

The reason it’s not included already is that 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. So either you’d  
have to do this check on instantiation (probably in ole\_set\_member(),  
the function that associates a WIN32OLE object with its COM object), or  
you could include Enumerable in WIN32OLE itself.

I’m not sure how expensive it would be in terms of performance, adding  
this check to ole\_set\_member().

Masaki S.?

Cheers,  
Dave

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 29, 2006, 2:28pm UTC](https://www.ruby-forum.com/t/enumerable-and-win32ole/70127/3 "2006-09-29T14:28:10Z")

</div>

Hello, sorry for being too late to reply.

In message “Re: Enumerable and WIN32OLE”  
on 06/09/27, Dave B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> Yes, I’ve expected that on occasion, and I think it would be a nifty  
> feature indeed. Enumerable’s methods don’t clash with common COM  
> collection methods, as far as I can see.

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),  
and you can use Enumerable’s methods by adding the following code.  
class WIN32OLE  
include Enumerable  
end

Regards,  
Masaki S.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 29, 2006, 3:32pm UTC](https://www.ruby-forum.com/t/enumerable-and-win32ole/70127/4 "2006-09-29T15:32:21Z")

</div>

In message “Re: Enumerable and WIN32OLE”  
on 06/09/29, Masaki S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> In message “Re: Enumerable and WIN32OLE”  
> on 06/09/27, Dave B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:
> 
> > Yes, I’ve expected that on occasion, and I think it would be a nifty  
> > feature indeed. Enumerable’s methods don’t clash with common COM  
> > collection methods, as far as I can see.
> 
> Enumerable#find method clashes the ‘find’ method of Excel Range object.  
> This is the (only) reason why WIN32OLE does not include Enumerable.

Enumerable#select method clashes the ‘select’ method of Excel Range  
object.  
And I think this case is more painful than the ‘find’ case.

Regards,  
Masaki S.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 29, 2006, 6:27pm UTC](https://www.ruby-forum.com/t/enumerable-and-win32ole/70127/5 "2006-09-29T18:27:12Z")

</div>

Masaki S. wrote:

> > Enumerable#find method clashes the ‘find’ method of Excel Range object.  
> > This is the (only) reason why WIN32OLE does not include Enumerable.
> 
> Enumerable#select method clashes the ‘select’ method of Excel Range object.  
> And I think this case is more painful than the ‘find’ case.

You’re right, we don’t want to break people’s range.find or range.select  
calls (at least in Ruby 1.8).

What about something like this, though? For these methods, a block is  
always provided. For a COM method call, a block is never provided. Why  
not dispatch depending on block\_given?

class WIN32OLE  
include Enumerable  
%w( find select ).each do |m|  
alias\_method “enum\_#{m}”, m  
define\_method m do |\*args|  
if block\_given?  
send(“enum\_#{m}”, \*args) {|\*a| yield \*a }  
else  
invoke m, \*args  
end  
end  
end  
end

My preference for writing code for OLE is to use uppercase method calls  
(range.Select) and save lowercase for Ruby methods. But this should  
provide compatibility for people who have range.select in their code.  
IEnums should have Enumerable.

Cheers,  
Dave

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 30, 2006, 3:50am UTC](https://www.ruby-forum.com/t/enumerable-and-win32ole/70127/6 "2006-09-30T03:50:55Z")

</div>

In message “Re: Enumerable and WIN32OLE”  
on 06/09/30, Dave B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> What about something like this, though? For these methods, a block is  
> always provided. For a COM method call, a block is never provided. Why  
> not dispatch depending on block\_given?

Some Enumerable’s methods does not need a block, for example,  
Enumerable#max.  
So, I hesitate to implement your idea.  
(I am not sure that there are OLE methods whose name is “max”.)

> My preference for writing code for OLE is to use uppercase method calls  
> (range.Select) and save lowercase for Ruby methods. But this should  
> provide compatibility for people who have range.select in their code.

I prefer this approach.  
In Win32OLE, WIN32OLE includes Enumerable.  
If you want to call original method(range.select),  
you should use range.Select.  
(And Win32OLE does not provide the compatibility.  
I have no idea to provide the compatibility.)

Or,  
In Win32OLE, WIN32OLE does not include Enumerable.  
If you want to use Enumerable’s method,  
you add the following code at your own risk.  
class WIN32OLE  
include Enumerable  
end

> IEnums should have Enumerable.

I agree with you, but I have no idea that all things work well.

Regards,  
Masaki S.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 30, 2006, 11:59pm UTC](https://www.ruby-forum.com/t/enumerable-and-win32ole/70127/7 "2006-09-30T23:59:14Z")

</div>

Hello,  
In message “Re: Enumerable and WIN32OLE”  
on 06/10/01, Dave B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> > Some Enumerable’s methods does not need a block, for example, Enumerable#max.  
> > So, I hesitate to implement your idea.  
> > (I am not sure that there are OLE methods whose name is “max”.)
> 
> I had an earlier version of the code I posted that went through  
> Enumerable.instance\_methods but skipped specific methods that don’t need  
> blocks (min, max, to\_a, entries, grep, sort, zip, /enum\_._/, /._?$/).  
> You can probably dismiss this option as well as what I wrote, just for  
> being so inconsistent. I don’t prefer this option, but I think it does  
> strike a valuable balance between POLS and compatibility.

I think I understand your code skip the methods that don’t need  
blocks.  
But, for example, if there is an OLE methods whose name is “max”,  
then, I am afraid that we might have the same problem.  
Which method does the following code call (in your posted code)?  
OLE method max or Enumerable#max?

ole = WIN32OLE.new(…)  
ole.max

Do I misunderstand you?

> Looking at where String’s going in Ruby (apparently becoming  
> non-Enumerable in 1.9) another option could be to leave Enumerable out  
> of WIN32OLE itself, but provide an enum method to return an enumerator  
> object:
> 
> class WIN32OLE  
> def enum  
> Enumerable::Enumerator.new(self)  
> end  
> end

Hmm, please give me some time to investigate it.  
(If there is OLE “enum” method, I think we might have the same problem  
…)

Regards,  
Masaki S.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 30, 2006, 5:06pm UTC](https://www.ruby-forum.com/t/enumerable-and-win32ole/70127/8 "2006-09-30T17:06:28Z")

</div>

Masaki S. wrote:

> In message “Re: Enumerable and WIN32OLE”  
> on 06/09/30, Dave B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:
> 
> > What about something like this, though? For these methods, a block is  
> > always provided. For a COM method call, a block is never provided. Why  
> > not dispatch depending on block\_given?
> 
> Some Enumerable’s methods does not need a block, for example, Enumerable#max.  
> So, I hesitate to implement your idea.  
> (I am not sure that there are OLE methods whose name is “max”.)

I had an earlier version of the code I posted that went through  
Enumerable.instance\_methods but skipped specific methods that don’t need  
blocks (min, max, to\_a, entries, grep, sort, zip, /enum\_._/, /._?$/).  
You can probably dismiss this option as well as what I wrote, just for  
being so inconsistent. I don’t prefer this option, but I think it does  
strike a valuable balance between POLS and compatibility.

> I prefer this approach.  
> In Win32OLE, WIN32OLE includes Enumerable.  
> If you want to call original method(range.select),  
> you should use range.Select.  
> (And Win32OLE does not provide the compatibility.  
> I have no idea to provide the compatibility.)

I don’t think this is a bad idea, but the incompatibility issue needs  
considering. I think it’s worth it.

> Or,  
> In Win32OLE, WIN32OLE does not include Enumerable.  
> If you want to use Enumerable’s method,  
> you add the following code at your own risk.  
> class WIN32OLE  
> include Enumerable  
> end

This (what we currently have) is deficient in the principle of least  
surprise and utility. You should not have to explicitly include or  
extend Enumerable on a WIN32OLE IEnum object.

Looking at where String’s going in Ruby (apparently becoming  
non-Enumerable in 1.9) another option could be to leave Enumerable out  
of WIN32OLE itself, but provide an enum method to return an enumerator  
object:

class WIN32OLE  
def enum  
Enumerable::Enumerator.new(self)  
end  
end

Cheers,  
Dave

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 1, 2006, 6:36am UTC](https://www.ruby-forum.com/t/enumerable-and-win32ole/70127/9 "2006-10-01T06:36:18Z")

</div>

Masaki S. wrote:

> Do I misunderstand you?  
> No, that’s right, that is still an issue, only for (min, max, entries,  
> sort, grep).

> Hmm, please give me some time to investigate it.  
> (If there is OLE “enum” method, I think we might have the same problem …)

I missed something in the Enumerator API: the method Object#to\_enum does  
what I intended “enum” to do.

Cheers,  
Dave
