From: "Carlo E. Prelz" Date: 2013-01-17T04:07:48+09:00 Subject: Re: Handling library versions Subject: Handling library versions Date: Thu 17 Jan 13 03:50:20AM +0900 Quoting Melanie Koller (lists@ruby-forum.com): > I am trying to implement a library that provides an unified way to > communicate to a server which is changing its API calls from version to > version. I plan to implement it that way: > > connect.rb: > > class Connect > def self.getConn(params) > case params[:version] > when '4.0' then return Connect4.new(params) > when '5.0' then return Connect5.new(params) > else 'Version not supported.' > end > > end > end > > class Connect4 > def initialize(params) > # Code for v4. > end > end > > class Connect5 > def initialize(params) > # Code for v5. > end > end One of the advantages of object orientation is inheritance - ideal in your case. I would do something like: class Connect def Connect::get_conn(params) case params[:version] when '4.0' return Connect4::new(params) when '5.0' return Connect5::new(params) end end raise "Version #{params[:version]} not supported." end def generic_func .. end end class Connect4 < Connect def initialize(params) # Code for v4. end def v4_specific_func .. end end class Connect5 < Connect def initialize(params) # Code for v5. end def v5_specific_func .. end def generic_func # overloads generic one end end The advantage is that instances of both Connect4 and Connect5 also respond to methods defined in Connect. You can thus neatly divide between things that are unique of each version, and things that are common among versions. Carlo -- * Se la Strada e la sua Virtu' non fossero state messe da parte, * K * Carlo E. Prelz - fluido@fluido.as che bisogno ci sarebbe * di parlare tanto di amore e di rettitudine? (Chuang-Tzu)