From: John Mair Date: 2010-10-31T05:06:44+09:00 Subject: Re: Dynamically reference instance vars You realize that instance_variable_set isn't private right? So you can simply go: my_object.instance_variable_set("@#{iname}", ivalue) Greg Willits wrote in post #958159: > 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/.