From: Jesús Gabriel y Galán on
On Thu, Jul 29, 2010 at 2:54 AM, <travis.ml-ruby-talk(a)subspacefield.org> wrote:
> So I have a class that I may want to instantiate multiple ways.
>
> Here's the signatures of the some so far:
>
>  def create_from_file(absolute_filename, archive_filename, fsg=nil, verbose=false, debug=false)
>  def create_from_parts(archive_filename, mtime, sha512hash)
>  def create_from_string(str)
[...]
> So the other way I looked into was to create some static or class
> methods which created the object myself, and avoid new entirely.
> After all, there's nothing too magic about new, and if it can't do
> anything too fancy, emulating polymorphism in a scripting language
> using branching and RTTI is totally lame.

> So I looked into this a bit, and I assume something like this could
> work:
>
>  def self.create_from_file(*args)
>      return self.new.create_from_file(*args)
>  end
>
> Then I realized that I've got six methods now to do initialization;
> three class methods (which are a little obscure to noobs) and three
> instance methods.

I wouldn't create the instance methods, and do everything in the class methods:

def self.create_from_file(absolute_filename, archive_filename,
fsg=nil, verbose=false, debug=false)
#whatever processing of the arguments and then
self.new standard_params_to_initialize
end

def self.create_from_parts(archive_filename, mtime, sha512hash)
#whatever processing of the arguments and then
self.new standard_params_to_initialize
end

and so on. If the code is repetitive you can refactor the common parts.

Jesus.