# How to wrap methods of WIN32OLE objects

**URL:** <https://www.ruby-forum.com/t/how-to-wrap-methods-of-win32ole-objects/96262>\
**Category:** Ruby\
**Created:** [June 27, 2006, 7:53pm UTC](https://www.ruby-forum.com/t/how-to-wrap-methods-of-win32ole-objects/96262 "2006-06-27T19:53:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![fxn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/77aa72/32.png) [@fxn](https://www.ruby-forum.com/u/fxn)\
**Post date:** [June 27, 2006, 7:53pm UTC](https://www.ruby-forum.com/t/how-to-wrap-methods-of-win32ole-objects/96262/1 "2006-06-27T19:53:57Z")

</div>

I am doing screen-scraping with Explorer:

WIN32OLE.new(‘InternetExplorer.Application’)

When you ask explorer to send some request and the method returns it  
may need yet some time to complete building the DOM etc., and you  
need to do something like:

def wait\_for\_completion  
sleep 1 while @ie.busy  
sleep 1 while @ie.readyState != 4  
sleep 1 while @ie.document.readyState != ‘complete’  
end

Since there is no class to subclass I tried to wrap the method to  
submit simple requests (Navigate) like this:

# untested

original\_navigate = @ie.method(:Navigate)  
def @ie.navigate(url)  
original\_navigate(url)  
wait\_for\_completion  
end

but it turns out that @ie is an object of class WIN32OLE that has  
no :Navigate method (so the program dies in that @ie.method  
(:Navigate)). Perhaps this has something to do with late binding. Do  
you know any way to do that?

– fxn

---

<div class="post-metadata">

**Author:** ![fxn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/77aa72/32.png) [@fxn](https://www.ruby-forum.com/u/fxn)\
**Post date:** [June 28, 2006, 12:28am UTC](https://www.ruby-forum.com/t/how-to-wrap-methods-of-win32ole-objects/96262/2 "2006-06-28T00:28:55Z")

</div>

Navigate is not a method. Instead it is handled by method\_missing which  
converts it to @ie.invoke(“Navigate”). Late binding, indeed.

Bret

---

<div class="post-metadata">

**Author:** ![fxn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/77aa72/32.png) [@fxn](https://www.ruby-forum.com/u/fxn)\
**Post date:** [June 27, 2006, 8:16pm UTC](https://www.ruby-forum.com/t/how-to-wrap-methods-of-win32ole-objects/96262/3 "2006-06-27T20:16:31Z")

</div>

On 6/27/06, Xavier N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am doing screen-scraping with Explorer:

Have you looked at the WATIR project ([http://wtr.rubyforge.org/](http://wtr.rubyforge.org/))? It’s  
intended as a way to test web applications on the client-side but it  
also  
scripts Internet Explorer very nicely. I bet it will even give you  
access to  
the DOM (plus a whole lot more). Sorry to not answer your question  
directly,  
but you may want to explore it and determine if you are reinventing the  
wheel.

but it turns out that @ie is an object of class WIN32OLE that has

> no :Navigate method (so the program dies in that @ie.method  
> (:Navigate)). Perhaps this has something to do with late binding. Do  
> you know any way to do that?

To address your code specifically, you can add methods to any object  
this  
way:

# Add a method to @ie object. Untested!

class \<\< @ie  
def nav\_and\_wait(url)  
@ie.Navigate(…)  
wait\_for\_completion  
end  
end

Because of the way those COM objects are built, you may have to add this  
method to each instance returned from WIN32OLE.new but that’s not too  
hard  
to do either. Note that I’m not overridding Navigate - I’m just adding a  
new  
method that wraps the call.

Justin

---

<div class="post-metadata">

**Author:** ![fxn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/77aa72/32.png) [@fxn](https://www.ruby-forum.com/u/fxn)\
**Post date:** [June 28, 2006, 12:44am UTC](https://www.ruby-forum.com/t/how-to-wrap-methods-of-win32ole-objects/96262/4 "2006-06-28T00:44:21Z")

</div>

On Jun 28, 2006, at 0:27, removed\_email\_address@domain.invalid wrote:

> Navigate is not a method. Instead it is handled by method\_missing  
> which  
> converts it to @ie.invoke(“Navigate”). Late binding, indeed.

Ah, thank you, now I see how it works.

– fxn

---

<div class="post-metadata">

**Author:** ![fxn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/77aa72/32.png) [@fxn](https://www.ruby-forum.com/u/fxn)\
**Post date:** [June 28, 2006, 12:50am UTC](https://www.ruby-forum.com/t/how-to-wrap-methods-of-win32ole-objects/96262/5 "2006-06-28T00:50:39Z")

</div>

On Jun 27, 2006, at 20:15, Justin B. wrote:

> Have you looked at the WATIR project ([http://wtr.rubyforge.org/](http://wtr.rubyforge.org/))? It’s  
> intended as a way to test web applications on the client-side but  
> it also  
> scripts Internet Explorer very nicely. I bet it will even give you  
> access to  
> the DOM (plus a whole lot more). Sorry to not answer your question  
> directly,  
> but you may want to explore it and determine if you are reinventing  
> the  
> wheel.

Oh great! I will base the project on this instead of using the raw  
object, they have already passed through the troubles I’ll discover :-).

– fxn
