On 12/3/06, paul <pjvleeuwen / gmail.com> wrote:
> :) thanks all for the educating responses. Martin, I really liked you
> suggestion.
>
>
> Is there any moral/principal reason to write:
>   A.a = "AA"
>   A.b = "BB"
> instead of:
>   A.set_b("BB").set_a("AA")

Actually, when I have this problem, I tend to use a third option:

class A
  def initialize(args)
    set args
  end

  def set(arg_hash)
    arg_hash.each_pair {|k, v|
      self.send(:"#{k}=", v)
    }
    self
  end
end

Now you can do a = A.new(:foo => "hello", :bar => "world").whatever
(assuming, of course, that you have appropriately called attr_writer
:foo, :bar or otherwise defined #foo= and #bar= first).

If you only want to set the instance variables when you create the
object, you can get rid of set and inline the code into initialize
instead.

martin