From: "David A. Black" Date: 2007-10-18T10:39:42+09:00 Subject: Re: abstract superclasses Hi -- On Thu, 18 Oct 2007, Yossef Mendelssohn wrote: > ]On Oct 17, 2:41 pm, "Thomas Adam" wrote: >> On 17/10/2007, Giles Bowkett wrote: >> >>> I sometimes use the Java thing of an abstract superclass in Ruby. I >>> usually just set up the abstract parent so that it'll break if used >>> directly. Is it better to just use Modules? Have other people run into >>> this? >> >> I'd use a Module, yes. Or a class: >> >> class SomeClassMimickingAbstract >> private_class_method :new >> end >> >> But you don't need to think this way in Ruby -- unlike Java, Ruby is >> weakly typed. >> >> -- Thomas Adam > > How do you suggest to use that class? I understanding keeping the > "abstract" class from being instantiated, but what about the concrete > classes, the ones you're going to use? > > class RealThing < SomeClassMimickingAbstract > # now make .new public again > end > > Ugly, isn't it? Of course, that's just more fuel for the "don't do > this in Ruby" fire. You could do: class C private_class_method :new def self.inherited(c) class << c; public :new; end end end I've actually never felt the need for abstract classes in Ruby programs. I'm not a Java programmer, so it never would have occurred to me in that context, and I haven't found myself inventing it for Ruby. David -- Upcoming training from Ruby Power and Light, LLC: * Intro to Ruby on Rails, Edison, NJ, October 23-26 * Advancing with Rails, Edison, NJ, November 6-9 Both taught by David A. Black. See http://www.rubypal.com for more info!