Multiple class initialization methods
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 Ruby, Rails | no comments |