Newbie question about dynamic class overriding

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

On Mar 29, 4:47 am, Emil [email protected] 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

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

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

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