Title / Description
Code class Vehicle def initialize(options); @options = options; end def self.factory(options) options.delete("class").new(options) end end class Bicycle < Vehicle; def honk; "Ring Ring"; end end class Train < Vehicle; def honk; @options["tone"] || "Woooooooo Woooooooo"; end end irb> Vehicle.factory("class" => Bicycle).honk => "Ring Ring" irb> Vehicle.factory("class" => Train, "tone" => "squeek").honk => "squeek"
Author
Highlight as C C++ CSS Clojure Delphi ERb Groovy (beta) HAML HTML JSON Java JavaScript PHP Plain text Python Ruby SQL XML YAML diff code