In article <200106101448.f5AEmT001285 / orsay1.moulon.inra.fr>,
ts <decoux / moulon.inra.fr> wrote:

> Just write
>
>   class A
>      attr_accessor :foo, :bar, :baz
>      def initialize(hash)
>         hash.each do |k, v|
>            eval "self.#{k}= #{v}"
>         end
>      end
>   end
> 
>   a = A.new "foo" => 12, "bar" => 24

It's tempting, but unfortunately it does not work in the general case.
Consider the following:

h = { "foo" => File.new("foo.txt"),
      "bar" => File.new("bar.txt"),
      "baz" => File.new("baz.txt") }

a = A.new(h)

Passing the values into eval will get you the string representations
of these File objects, which is obviously not what you want.  That's
why the call to these accessors via send is necessary, so that the
values will arrive in those methods unchanged.  It's a bit more hair,
which is why I suggested encapsulating it in a separate module to
avoid duplication of code.

--Mirian