David Naseby wrote:
> class SomeClass
>   def initialize( h )
>     h.each do | key, val | 
>       instance_variable_set( "@#{key}".intern, val )
>     end
>   end
> end

Also, if you want others to be able to access those instance variables, you'll probably want to define readers and/or writers for them.

The code I came up with before David beat me to the punch was ...

class SomeClass
  def initialize(h)
    h.each do |key, value|
      instance_eval %{
        @#{key} = value

        def #{key}
          @#{key}
        end
      }
    end
  end
end

I tried to replace the "def #{key} ..." with "attr_reader :#{key}.intern", but that didn't work, for some reason that I don't have time to look at.  I'd be interested to hear from anyone who can explain why it doesn't.

By the way, unless I'm mistaken, neither of these will work properly if the hash's keys are something other than strings or symbols (ie, things for which #{key} generates a simple string).  However, I'm guessing that's all you intended them to be.

Harry O.