Robert Klemme
3/1/2005 10:40:00 AM
"gga" <GGarramuno@aol.com> schrieb im Newsbeitrag
news:1109671603.644722.113290@g14g2000cwa.googlegroups.com...
> Thanks, Florian and Robert. I had indeed ended up doing refactoring as
> the easiest solution.
>
>
> However, I admit not being too crazy about that as a general solution,
> as that can easily lead to needing tons of, relatively speaking,
> "useless" a_impl()-like methods on complex inheritance chains. Not
> that it has been a problem for me yet, but... makes me go ...hmm...
>
> Glad to know there is at least some obscure way of going around it,
> too.
>
> Also, I'm not sure I understand your comment about runtime changes.
> How would you work around the issue if A was not ruby code. Would you
> create an additional intermediate bridge class in ruby and have B
> inherit from it?
I'm not 100% sure but this should work - even if A was not completely
implemented in Ruby:
class A
alias :a_impl :a
end
Then all sub classes can access a_impl - unless it's overridden of course.
:-)
An additional point of clarification: I do not mandate to have a and
a_impl for each public method. Instead I suggest to proper distribute
functionality across methods (with appropriate names). I know that this
is not always simple but it's good to keep that in mind when designing
base classes. Writing methods that do too much yields usually bad
modularity.
An example from a recent thread: someone suggested a method to cat input
to output. The suggested solution opened two (or more) files and copied
data from one file to the other. IMHO it's better to have separate
methods that do the file handling and the copying, with the copying method
working on open streams (IO). That way the copying can be reused in a
much wider range of applications.
# monolithic
def file_copy(out, *in)
File.open(out, "wb") do |o|
in.each do |fin|
File.open(fin,"rb") do |i|
while ( b = i.read(1024) )
o.write b
end
end
end
end
end
# modular
def stream_copy(in, out, size=1024)
while ( b = in.read(size) )
out.write b
end
end
def file_copy(out, *in)
File.open(out, "wb") do |o|
in.each do |fin|
File.open(fin, "rb") do |i|
stream_copy(i, o)
end
end
end
end
Kind regards
robert