On Thursday, August 2, 2012 12:14:27 PM UTC-4, Robert Klemme wrote:
>
> On Thu, Aug 2, 2012 at 4:47 PM, Intransition <[email protected]> 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? 
>

Yes, but it is actually via YAML file. It will read in, eg.

  ---
  revision: 1
  ...

Then when it does a `Main.load` on the file, it will look for the 
`revision` field and determine which version of the API to use.

 

> 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. 
>

Adding some convenience, nice. I like autoload too, but word has it, it is 
being deprecated (albeit in the distant future).

-- You received this message because you are subscribed to the Google Groups 
ruby-talk-google group. To post to this group, send email to 
[email protected]. To unsubscribe from this group, send email 
to [email protected]. For more options, visit this 
group at https://groups.google.com/d/forum/ruby-talk-google?hl=en

Reply via email to