From: Suman Gurung Date: 2009-01-02T02:46:51+09:00 Subject: Re: How do I pass the name of an instance variable into an object? i just tried putting everything together and it worked great. I thought i will share the consolidated code, so here it is: class SomeClass def initialize @first = 'first variable' @second = 'second variable' end def print_instance_variable(*args) args.each do |arg| begin raise unless instance_variable_defined?("@#{arg}") puts self.instance_variable_get("@#{arg}") rescue => ex puts "#{ex.class}: #{ex.message}" end end end def add_instance_variable(name, initial_value) raise if instance_variable_defined?("@#{name}") self.instance_variable_set("@#{name}", "#{initial_value}") end end x = SomeClass.new x.print_instance_variable(:first) x.print_instance_variable(:second) puts "\n****************** printing both values in one method call *************" x.print_instance_variable :first, :second puts "\n*************** should throw an exception, because the instance variable is undefined **************" x.print_instance_variable :third x.add_instance_variable(:third, "third value") puts "\n*************** should print the third instance variable**************" x.print_instance_variable :third