Method switching

L

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

thanks in advance..
 
R

Robert Klemme

2007/9/5 said:
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
 
L

Lee Jarvis

Robert said:
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..
 
S

Sebastian Hungerecker

Lee said:
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
 
S

Sebastian Hungerecker

Sebastian said:
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?
 
R

Robert Klemme

2007/9/5 said:

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

def some_method
...
wrap_send("foo")
...
end
Yeah i know.. it's not actually send, that was just an example



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
 
L

Lee Jarvis

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
 

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
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top