From: Mark Wilson Date: 2003-06-12T05:50:56+09:00 Subject: Re: Multiple Initialize methods? > On Thu, 12 Jun 2003 04:54:59 +0900, Nick wrote: >> Hi, >> >> I need to create a class either with a param in the construction >> or nothing: >> >> something = Object.new >> or >> something = Object.new(val1, val2) >> >> [snip] It occurs to me that a standard way to do something like this in Ruby is as follows (taken from set.rb): The code below gives two methods to create the object: Set.new [returns empty set] Set.new([_array_]) [returns a set, each element of which is an element of the given array (without duplicates)] Set.new({_hash_}) [returns a set, each element of which is an array consisting of the given key-value pairs] Set[_array_] [returns a set, each element of which is an element of the given array (without duplicates)] Set.[](_elements_) [returns a set, each element of which is one of the given elements (without duplicates)] Code from set.rb: # Creates a new set containing the given objects. [Notice the call to the 'new' method.] def self.[](*ary) new(ary) end # Creates a new set containing the elements of the given enumerable # object. # # If a block is given, the elements of enum are preprocessed by the # given block. def initialize(enum = nil, &block) # :yields: o @hash ||= Hash.new enum.nil? and return if block enum.each { |o| add(block[o]) } else merge(enum) end end