From: "Jesús Gabriel y Galán" Date: 2010-10-31T04:33:49+09:00 Subject: Re: Dynamically reference instance vars On Sat, Oct 30, 2010 at 9:27 PM, Greg Willits wrote: > Appreciate the effort, but the point has been lost. Don't focus on the > hash or mass assignment, that was just a device to create an example. > > The question is whether there's a shorter way to do this: > >  my_object.send(:instance_variable_set, "@#{iname}", ivalue) > > In another language I used, I could simply do the equivalent of > >  myobject.iname = ivalue > > that's because the syntax was like this >  #myobject->#iname = ivalue > > Where # denotes a local var, so it was obvious to the parser that #iname > was not a method name, whereas with Ruby it's not obvious. > > I was hoping in Ruby that at least this was possible: > >  myobject.send(iname) = ivalue >  --or-- >  myobject.send(iname, ivalue) > > but that doesn't work. I could maybe force it to work by manually > creating setter methods, but that's not an elegant solution either, I'd > rather have the occassional use of instance_variable_set than craft > setters. > > Anyway, not a biggie, just a curiosity. Crafting setters is pretty simple, though: class A attr_writer :a end a = A.new a.a = 3 Jesus.