From: Greg Willits Date: 2010-10-30T11:14:50+09:00 Subject: Dynamically reference instance vars If I need to dynamically reference instance vars, is this the only way to do it (var set example)? my_object.send(:instance_variable_set, "@#{iname}", ivalue) I expected something more elegant, but this is the only way I can get it to work. No biggie, just curious. More complete example below. -- gw class Shape attr_accessor :size, :fill_color, :line_color, :line_width def initialize @size = "" @fill_color = "" @line_color = "" @line_width = "" end end my_shape = Shape.new shape_details = { :size => 'small', :fill_color => 'red', :line_color => 'black', :line_width => '2'} shape_details.each do |iname, ivalue| my_shape.send(:instance_variable_set, "@#{iname}", ivalue) end -- Posted via http://www.ruby-forum.com/.