From: Raul Parolari Date: 2007-11-12T13:02:29+09:00 Subject: Re: name of a variable, at runtime Colin Summers wrote: > This seems harder than the usual thing I've been tripping on (and > finding answers in the pickaxe or online). > > I have an object: human. It has parts: human.hand human.foot. > > How can I say this: > fred = human.new > fred.hand = fred.foot = fred. leg = 'flesh' > fred.knee = 'plastic' > fred.head = 'steel' > parts = [hand,foot,leg,knee] > parts.each do |part| > puts "part #{part} is made of " + fred.{part}.to_s > end # with each part > > Am I close? > > Thanks, > --Colin I think that 'object.send(method)' gives you what you want: class Human attr_accessor :hand, :foot, :leg, :knee, :head end fred = Human.new fred.hand = fred.foot = fred. leg = 'flesh' fred.knee = 'plastic' fred.head = 'steel' parts = %w[hand foot leg knee] parts.each do |part| puts "part #{part} is made of " + fred.send(part) end Raul -- Posted via http://www.ruby-forum.com/.