From: Robert Klemme Date: 2012-08-03T01:14:27+09:00 Subject: Re: Supporting multiple versions of an API On Thu, Aug 2, 2012 at 4:47 PM, Intransition wrote: > Yes, that's the traditional factory approach. I actually would not want to > use "MainFactory", as don't want it to be explicit. But it occurs to me that > I could have redefined `Main.new` as a factory method and done it that way. > And the more I think about it the more that seems like a better approach. It > would work well for Main as well as other classes within it. It seems that in your case the caller decides on the version. Is that correct? What about this? file mylib.rb: module MyLib # manual autoload :V0 'mylib/v0' autoload :V1 'mylib/v1' # generated %w{V0 V1}.each do |ver| autoload ver.to_sym "mylib/#{ver.downcase}" end # the default Default = V1 # to make access to the current version easy: def self.const_missing(c) Default.const_get(c) end end and in mylib/v0 etc. module MyLib module V0 class Main; end end end Usage # current m = MyLib::Main.new(...) # explicit m = MyLib::V0::Main.new(...) The const_missing trick works only if there are no name conflicts, of course. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/