Newbie question about dynamic class overriding

E

Emil

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
 
C

ChrisH

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

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
 
G

Gary Wright

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.

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
 
I

Ilan Berci

Emil said:
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
 
I

Ilan Berci

Ilan said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top