Ruby Findings

digging for gems

Multiple class initialization methods

Posted by Art Green Thu, 06 Sep 2007 01:18:00 GMT

Here’s a quick run-down on the development of multiple initialize methods for Ruby classes: Multiple Initialize Methods.

The author looks at the subject from a domain language idiom perspective. I used the information to provide multiple creation interfaces for a class.

Quick example:

Class Archive
  def Archive.create( archive_name )
    return self.new( archive_name, :create )
  end

  def Archive.open( archive_name )
    return self.new( archive_name, :open )
  end

  def initialize( archive_name, mode )
    # do interesting stuff
  end
end

arch = Archive.open("some_existing_file")
arch = Archive.create("some_new_file")

Posted in , | no comments |


html>