This post originated from an RSS feed registered with Ruby Buzz
by Daniel Berger.
Original Post: Keep your classes subclassable!
Feed Title: Testing 1,2,3...
Feed URL: http://djberg96.livejournal.com/data/rss
Feed Description: A blog on Ruby and other stuff.
Evan Phoenix submitted a patch to ruby-core recently that caused me have one of those "duh!" moments, accompanied by a smack to the forehead. What am I talking about? I'm talking about not hard coding your class names, thus making your classes difficult or impossible to subclass.
Consider this example:
class Foo
def create_new
Foo.new
end
end
Zip it and ship it, right? Except, a short time later, someone uses your code and decides they want to subclass Foo:
class Bar < Foo
end
Bar.new.create_new # Oops!
See the problem? They're going to get back a Foo class instead of a Bar class because you hard coded the class name in the method.