Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Method switching

Reply
Thread Tools

Method switching

 
 
Lee Jarvis
Guest
Posts: n/a
 
      09-05-2007
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/.

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      09-05-2007
2007/9/5, Lee Jarvis <(E-Mail Removed)>:
> 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

 
Reply With Quote
 
 
 
 
Lee Jarvis
Guest
Posts: n/a
 
      09-05-2007
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/.

 
Reply With Quote
 
Sebastian Hungerecker
Guest
Posts: n/a
 
      09-05-2007
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: http://www.velocityreviews.com/forums/(E-Mail Removed)
ICQ: 205544826

 
Reply With Quote
 
Sebastian Hungerecker
Guest
Posts: n/a
 
      09-05-2007
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: (E-Mail Removed)
ICQ: 205544826

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      09-05-2007
2007/9/5, Lee Jarvis <(E-Mail Removed)>:
> 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

 
Reply With Quote
 
Lee Jarvis
Guest
Posts: n/a
 
      09-05-2007
> 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/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
method def in method vs method def in block Kyung won Cheon Ruby 0 11-21-2008 08:48 AM
Process Switching vs. Fast/CEF Switching? asdf Cisco 7 05-29-2007 05:26 PM
Catalyst 3500 switching method thefunnel@aol.com Cisco 1 07-05-2005 04:40 PM
Switching AC to Battery loses wireless IP address =?Utf-8?B?c2tpYnVtNDQy?= Wireless Networking 6 10-28-2004 02:43 AM
Re: auto switching from LAN to Wifi connection Jeff Durham Wireless Networking 0 09-22-2004 05:39 PM



Advertisments