# Newbie question about dynamic class overriding

**URL:** <https://www.ruby-forum.com/t/newbie-question-about-dynamic-class-overriding/90498>\
**Category:** Ruby\
**Created:** [March 29, 2007, 10:47am UTC](https://www.ruby-forum.com/t/newbie-question-about-dynamic-class-overriding/90498 "2007-03-29T10:47:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![emil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f4b2a3/32.png) [@emil](https://www.ruby-forum.com/u/emil)\
**Post date:** [March 29, 2007, 10:47am UTC](https://www.ruby-forum.com/t/newbie-question-about-dynamic-class-overriding/90498/1 "2007-03-29T10:47:18Z")

</div>

Hi, I have a little problem which I don’t think shouldn’t be to  
difficult to solve.

I run two different methods. In one of the methods I want to override a  
class, but not in the other one.

I want to do something like this:

@auto = WIN32OLE.new(“AutoItX3.Control”)

def sign\_with\_CA\_one  
@auto.Send("{a}")  
@auto.Send("{b}")  
@auto.Send("{1}")  
@auto.Send("{2}")  
end

def sign\_with\_CA\_two  
class WIN32OLE  
alias oldSend(str) Send(str)  
def Send(str)  
oldSend(str)  
sleep 0.1  
end  
end  
@auto.Send("{a}")  
@auto.Send("{b}")  
@auto.Send("{1}")  
@auto.Send("{2}")  
end

In short: When using one of the methods I want to add a little sleep  
after each character sendt, but not in the other one. But the code above  
gives syntax error.

Any ideas?  
Regards  
Emil

---

<div class="post-metadata">

**Author:** ![emil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f4b2a3/32.png) [@emil](https://www.ruby-forum.com/u/emil)\
**Post date:** [March 29, 2007, 4:11pm UTC](https://www.ruby-forum.com/t/newbie-question-about-dynamic-class-overriding/90498/2 "2007-03-29T16:11:06Z")

</div>

On Mar 29, 4:47 am, Emil [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> def sign\_with\_CA\_one  
> oldSend(str)  
> after each character sendt, but not in the other one. But the code above  
> gives syntax error.
> 
> Any ideas?  
> Regards  
> Emil
> 
> –  
> Posted viahttp://www.ruby-forum.com/.

Send is not a method of WIN32OLE. WIN32OLE is delegating to the OLE  
object, AutoIt in this case.  
I suspect you have to write a wrapper instead of aliasing, but maybe  
someone with more OLE  
experience can provide a better solution

Cheers  
Chris

---

<div class="post-metadata">

**Author:** ![emil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f4b2a3/32.png) [@emil](https://www.ruby-forum.com/u/emil)\
**Post date:** [March 29, 2007, 5:14pm UTC](https://www.ruby-forum.com/t/newbie-question-about-dynamic-class-overriding/90498/3 "2007-03-29T17:14:18Z")

</div>

On Mar 29, 2007, at 4:47 AM, Emil wrote:

> ```
> @auto.Send("{1}")
> @auto.Send("{2}")
> 
> ```
> 
> end
> 
> In short: When using one of the methods I want to add a little sleep  
> after each character sendt, but not in the other one. But the code  
> above  
> gives syntax error.

You can’t put class blocks inside method definitions. That is why you  
are getting a syntax error. Just move it to the top level and define  
an alternate method to be used by sign\_with\_CA\_two

class WIN32OLE  
def slow\_send(str)  
send(str)  
sleep 0.1  
end  
end

def sign\_with\_CA\_two  
@auto.slow\_send("{a}")  
#…  
end

---

<div class="post-metadata">

**Author:** ![emil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f4b2a3/32.png) [@emil](https://www.ruby-forum.com/u/emil)\
**Post date:** [March 29, 2007, 5:53pm UTC](https://www.ruby-forum.com/t/newbie-question-about-dynamic-class-overriding/90498/4 "2007-03-29T17:53:07Z")

</div>

Ilan B. wrote:

> irb(main):001:0\> class A  
> irb(main):002:1\> def send; “send”; end  
> irb(main):003:1\> end  
> =\> nil  
> irb(main):004:0\> a = A.new  
> =\> #\<A:0x38665c\>  
> irb(main):005:0\> def a.send; super + ‘\_version2’ ; end  
> =\> nil  
> irb(main):006:0\> a.send  
> =\> “send\_version2”  
> irb(main):007:0\> A.new.send  
> =\> “send”

Emil,

My apologies, your title asked to do this dynamically, so this is  
probably what you want:

irb(main):001:0\> class A  
irb(main):002:1\> end  
=\> nil  
irb(main):003:0\> A.module\_eval(%q{class \<\< self; def send2(str); str;  
end; end})=\> nil  
irb(main):004:0\> A.send2  
ArgumentError: wrong number of arguments (0 for 1)  
from (irb):4:in `send2’  
from (irb):4  
from :0  
irb(main):005:0\> A.send2(“whoops”)  
=\> “whoops”

your question is a little confusing as class definition itself is  
dynamic in ruby… but I guess this is what you want by adding a class  
method “dynamically”

ilan

---

<div class="post-metadata">

**Author:** ![emil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f4b2a3/32.png) [@emil](https://www.ruby-forum.com/u/emil)\
**Post date:** [March 29, 2007, 5:34pm UTC](https://www.ruby-forum.com/t/newbie-question-about-dynamic-class-overriding/90498/5 "2007-03-29T17:34:39Z")

</div>

Emil wrote:

> def sign\_with\_CA\_two  
> class WIN32OLE  
> alias oldSend(str) Send(str)  
> def Send(str)  
> oldSend(str)  
> sleep 0.1  
> end  
> end  
> @auto.Send("{a}")  
> @auto.Send("{b}")  
> @auto.Send("{1}")  
> @auto.Send("{2}")  
> end

Emil,

While I am not certain, it seems like a singleton method may be the  
approach you are looking for:

irb(main):001:0\> class A  
irb(main):002:1\> def send; “send”; end  
irb(main):003:1\> end  
=\> nil  
irb(main):004:0\> a = A.new  
=\> #\<A:0x38665c\>  
irb(main):005:0\> def a.send; super + ‘\_version2’ ; end  
=\> nil  
irb(main):006:0\> a.send  
=\> “send\_version2”  
irb(main):007:0\> A.new.send  
=\> “send”

just define a singleton method in the instance that you want to have  
alternate behavior…

If you really need the inheritance case, then you can use the following:

irb(main):008:0\> class B \< A  
irb(main):009:1\> def send; super + ‘\_versionB’; end  
irb(main):010:1\> end  
=\> nil  
irb(main):011:0\> B.new.send  
=\> “send\_versionB”

hope this helps
