Method switching

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…

Could you use a hash defined just outside the something methods?

On 9/5/07, Lee J. [email protected] wrote:

end
for example i could use
thanks in advance…

Posted via http://www.ruby-forum.com/.


“Hey brother Christian with your high and mighty errand, Your actions
speak
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

I’m not sure I entirely understand what you are driving at… But if I
do,
you have a couple of options. The first is to use case/when. The
second is
to use metaprogramming, something Ruby lends itself to. I’m no master
of
that voodoo, but I’m sure someone else can chime in who is more familiar
:slight_smile:
If you could give a clearer example of what you need, it might be
easier,
too.

tom

2007/9/5, Lee J. [email protected]:

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 K. 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…

2007/9/5, Lee J. [email protected]:

end
for example i could use
thanks in advance…

Posted via http://www.ruby-forum.com/.

Maybe you should do a exception which raises when @color = 1 and catch
it
and do in “catch” block a wrapper?

Yours,
Ravalas.

Lee J. wrote:

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

2007/9/5, Lee J. [email protected]:

Robert K. 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

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

Sebastian H. wrote:

end

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?