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
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
If you could give a clearer example of what you need, it might be
easier,
too.
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.
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…
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
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.