Newbie question about dynamic class overriding

Discussion in 'Ruby' started by Emil, Mar 29, 2007.

  1. Emil

    Emil Guest

    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

    --
    Posted via http://www.ruby-forum.com/.
     
    Emil, Mar 29, 2007
    #1
    1. Advertising

  2. Emil

    ChrisH Guest

    On Mar 29, 4:47 am, Emil <> wrote:
    > 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
    >
    > --
    > 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
     
    ChrisH, Mar 29, 2007
    #2
    1. Advertising

  3. Emil

    Gary Wright Guest

    On Mar 29, 2007, at 4:47 AM, 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
    >
    > 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
     
    Gary Wright, Mar 29, 2007
    #3
  4. Emil

    Ilan Berci Guest

    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

    --
    Posted via http://www.ruby-forum.com/.
     
    Ilan Berci, Mar 29, 2007
    #4
  5. Emil

    Ilan Berci Guest

    Ilan Berci 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

    --
    Posted via http://www.ruby-forum.com/.
     
    Ilan Berci, Mar 29, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,943
    Thomas Weidenfeller
    Oct 12, 2005
  2. Replies:
    2
    Views:
    388
  3. Eric Baker
    Replies:
    4
    Views:
    320
    Ben Finney
    Nov 16, 2003
  4. Replies:
    2
    Views:
    566
  5. Replies:
    2
    Views:
    302
Loading...

Share This Page