Method switching

Discussion in 'Ruby' started by Lee Jarvis, Sep 5, 2007.

  1. Lee Jarvis

    Lee Jarvis Guest

    Ok this probably won't make any send at all, but lets say i have
    something like this:

    def send(a)
    puts "#{a}\n"
    end

    def colorsend(a)
    puts "#{colorize(a)}\n"
    end

    def something
    send "hello"
    end



    Ok lets say i have 50+ methods like the something method, that relate to
    the send method, i want to be able to have 1 switch to change the method
    without changing every method that uses send(message)
    for example i could use

    if @color = 1
    colorsend(msg)
    else
    send(msg)
    end

    but that means i would have to add that to *every* method.. if anyone at
    all out there understands what i mean it would be very helpful

    thanks in advance..
    --
    Posted via http://www.ruby-forum.com/.
     
    Lee Jarvis, Sep 5, 2007
    #1
    1. Advertising

  2. 2007/9/5, Lee Jarvis <>:
    > Ok this probably won't make any send at all, but lets say i have
    > something like this:
    >
    > def send(a)
    > puts "#{a}\n"
    > end
    >
    > def colorsend(a)
    > puts "#{colorize(a)}\n"
    > end
    >
    > def something
    > send "hello"
    > end
    >
    >
    >
    > Ok lets say i have 50+ methods like the something method, that relate to
    > the send method, i want to be able to have 1 switch to change the method
    > without changing every method that uses send(message)
    > for example i could use
    >
    > if @color = 1
    > colorsend(msg)
    > else
    > send(msg)
    > end
    >
    > but that means i would have to add that to *every* method.. if anyone at
    > all out there understands what i mean it would be very helpful


    Since you are dispatching based on an instance variable, why don't you
    wrap send and colorsend in another method that does the dispatching
    and invoke this other method from your 50+ methods?

    Btw, send is a bad name to choose because this is a standard method.

    I also wonder why you have 50+ methods that are somehow similar. It
    seems there is room for improvement. Maybe you can state the business
    problem you are trying to solve.

    Cheers

    robert
     
    Robert Klemme, Sep 5, 2007
    #2
    1. Advertising

  3. Lee Jarvis

    Lee Jarvis Guest

    Robert Klemme wrote:
    > Since you are dispatching based on an instance variable, why don't you
    > wrap send and colorsend in another method that does the dispatching
    > and invoke this other method from your 50+ methods?


    how so?

    > Btw, send is a bad name to choose because this is a standard method.


    Yeah i know.. it's not actually send, that was just an example


    > I also wonder why you have 50+ methods that are somehow similar.


    Who said they were similar?

    They all print different data to the console.. I just want to give the
    user the choice, /SET COLOR=1 or something similar and the rest of the
    data becomes colored..

    Maybe i should not be printing data from every different method, if i
    was only returning data then i suppose i could control the output type
    in one method..

    Thanks for the help also..
    --
    Posted via http://www.ruby-forum.com/.
     
    Lee Jarvis, Sep 5, 2007
    #3
  4. Lee Jarvis wrote:
    > Ok this probably won't make any send at all, but lets say i have
    > something like this:
    >
    > def send(a)
    > puts "#{a}\n"
    > end
    >
    > def colorsend(a)
    > puts "#{colorize(a)}\n"
    > end
    >
    > def something
    > send "hello"
    > end
    >
    >
    >
    > Ok lets say i have 50+ methods like the something method, that relate to
    > the send method, i want to be able to have 1 switch to change the method
    > without changing every method that uses send(message)


    You could just redefine the method in question, like this:

    def color_off()
    def my_print(a)
    puts a
    end
    end

    def color_on()
    def my_print(a)
    puts colorize(a)
    end
    end

    def something()
    my_print "hello"
    end

    color_off()
    something() #no color
    color_on()
    something() #color


    HTH,
    Sebastian
    --
    NP: Ulver - Dressed in Black
    Jabber:
    ICQ: 205544826
     
    Sebastian Hungerecker, Sep 5, 2007
    #4
  5. Sebastian Hungerecker wrote:
    > Lee Jarvis wrote:
    > > Ok this probably won't make any send at all, but lets say i have
    > > something like this:
    > >
    > > def send(a)
    > > puts "#{a}\n"
    > > end
    > >
    > > def colorsend(a)
    > > puts "#{colorize(a)}\n"
    > > end
    > >
    > > def something
    > > send "hello"
    > > end
    > >
    > >
    > >
    > > Ok lets say i have 50+ methods like the something method, that relate to
    > > the send method, i want to be able to have 1 switch to change the method
    > > without changing every method that uses send(message)

    >
    > You could just redefine the method in question


    Or you could, of course, just check some var inside inside of the method, like

    def send(a)
    if @color
    puts colorize(a)
    else
    puts a
    end
    end

    Or did I totally misunderstand what you're trying to do?


    --
    NP: Arcturus - Ad Absurdum
    Jabber:
    ICQ: 205544826
     
    Sebastian Hungerecker, Sep 5, 2007
    #5
  6. 2007/9/5, Lee Jarvis <>:
    > Robert Klemme wrote:
    > > Since you are dispatching based on an instance variable, why don't you
    > > wrap send and colorsend in another method that does the dispatching
    > > and invoke this other method from your 50+ methods?

    >
    > how so?


    def wrap_send(m)
    if @color = 1
    colorsend(m)
    else
    send(m)
    end
    end

    def some_method
    ...
    wrap_send("foo")
    ...
    end

    > > Btw, send is a bad name to choose because this is a standard method.

    >
    > Yeah i know.. it's not actually send, that was just an example
    >
    >
    > > I also wonder why you have 50+ methods that are somehow similar.

    >
    > Who said they were similar?


    It seemed so since all of them need that printing to be done. You
    might get better results when refactoring your code.

    > They all print different data to the console.. I just want to give the
    > user the choice, /SET COLOR=1 or something similar and the rest of the
    > data becomes colored..
    >
    > Maybe i should not be printing data from every different method, if i
    > was only returning data then i suppose i could control the output type
    > in one method..


    Maybe. I can't tell because I still have too little information.

    > Thanks for the help also..


    ywc

    Kind regards

    robert
     
    Robert Klemme, Sep 5, 2007
    #6
  7. Lee Jarvis

    Lee Jarvis Guest

    > def wrap_send(m)
    > if @color = 1
    > colorsend(m)
    > else
    > send(m)
    > end
    > end
    >


    Ahh crap i didn't even think of doing that, that's exactly what i have
    done now, sorry for the time wasting, noobish mistake on my behalf..

    Thanks a lot guys
    --
    Posted via http://www.ruby-forum.com/.
     
    Lee Jarvis, Sep 5, 2007
    #7
    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. MSNews

    Switching JITs

    MSNews, Jun 24, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    455
    Richard Grimes [MVP]
    Jun 30, 2004
  2. Abdul K Shaik

    DDR/SDR-SDRAM Bank Switching Doubt

    Abdul K Shaik, Aug 9, 2003, in forum: VHDL
    Replies:
    1
    Views:
    2,277
    David Jones
    Aug 9, 2003
  3. Simone Winkler

    switching problem

    Simone Winkler, Sep 5, 2003, in forum: VHDL
    Replies:
    5
    Views:
    606
    William Wallace
    Sep 10, 2003
  4. john
    Replies:
    4
    Views:
    521
    Roberto
    Dec 15, 2004
  5. Kyung won Cheon
    Replies:
    0
    Views:
    230
    Kyung won Cheon
    Nov 21, 2008
Loading...

Share This Page