19
What is the slickest, most Ruby-like way to have a single constructor return an object of the appropriate type?
To be more specific, here's a dummy example: say I have two classes Bike and Car which subclass Vehicle. I want this:
Vehicle.new('mountain bike') # returns Bike.new('mountain bike')
Vehicle.new('ferrari') # returns Car.new('ferrari')
I've proposed a solution below, but it uses allocate which seems way too implementation-heavy. What are some other approaches, or is mine actually ok?