# How do I pass the name of an instance variable into an object?

**URL:** <https://www.ruby-forum.com/t/how-do-i-pass-the-name-of-an-instance-variable-into-an-object/156693>\
**Category:** Ruby\
**Created:** [January 1, 2009, 4:20am UTC](https://www.ruby-forum.com/t/how-do-i-pass-the-name-of-an-instance-variable-into-an-object/156693 "2009-01-01T04:20:15Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [January 1, 2009, 4:20am UTC](https://www.ruby-forum.com/t/how-do-i-pass-the-name-of-an-instance-variable-into-an-object/156693/1 "2009-01-01T04:20:15Z")

</div>

Hi,

Suppose I wanted to create a method for an object that allowed me to  
pass in the name of an instance variable and to perform some task on the  
variable. For the sake of simplicity, let’s say that I’d like to be  
able to print out the value of the specified instance variable. Also,  
I’d want the method to raise an exception if the name of the variable  
getting passed into the object is not actually an instance variable in  
the object.

The class would look something like this:

class SomeClass  
def initialize  
@a = 1  
@b = ‘xxx’  
end

def print\_instance\_variables(obj)  
# some code goes here  
end  
end

And here’s how I’d like to be able to use it:

x = SomeClass.new  
x.print\_instance\_variables(:a) # writes the value of @a to the console  
x.print\_instance\_variables(:b) # writes the value of @b to the console  
x.print\_instance\_variables(:a, :b) # writes the values of @a and @b to  
the console  
x.print\_instance\_variables(:z) # causes an exception because there is  
no@z

Any suggestions would be greatly appreciated.

Thanks,

Glenn

---

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [January 1, 2009, 5:52am UTC](https://www.ruby-forum.com/t/how-do-i-pass-the-name-of-an-instance-variable-into-an-object/156693/2 "2009-01-01T05:52:07Z")

</div>

Glenn,

There may be a much better way to do this, but here’s what I threw  
together:

class SomeClass  
attr\_accessor :a, :b  
def initialize  
@a = ‘A’  
@b = ‘B’  
end

def print\_instance\_variable(variable)  
if instance\_variables.include?(“@#{variable}”)  
puts self.send(“#{variable}”)  
else  
raise ‘Not an instance variable.’  
end  
end  
end

x = SomeClass.new  
x.print\_instance\_variable(‘a’)

Hope that at least points you in the right direction.

– Josh  
[http://iammrjoshua.com](http://iammrjoshua.com)

Glenn wrote:

> Hi,
> 
> Suppose I wanted to create a method for an object that allowed me to  
> pass in the name of an instance variable and to perform some task on the  
> variable. For the sake of simplicity, let’s say that I’d like to be  
> able to print out the value of the specified instance variable. Also,  
> I’d want the method to raise an exception if the name of the variable  
> getting passed into the object is not actually an instance variable in  
> the object.
> 
> The class would look something like this:
> 
> class SomeClass  
> def initialize  
> @a = 1  
> @b = ‘xxx’  
> end
> 
> def print\_instance\_variables(obj)  
> # some code goes here  
> end  
> end
> 
> And here’s how I’d like to be able to use it:
> 
> x = SomeClass.new  
> x.print\_instance\_variables(:a) # writes the value of @a to the console  
> x.print\_instance\_variables(:b) # writes the value of @b to the console  
> x.print\_instance\_variables(:a, :b) # writes the values of @a and @b to  
> the console  
> x.print\_instance\_variables(:z) # causes an exception because there is  
> no@z
> 
> Any suggestions would be greatly appreciated.
> 
> Thanks,
> 
> Glenn

---

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [January 1, 2009, 6:49am UTC](https://www.ruby-forum.com/t/how-do-i-pass-the-name-of-an-instance-variable-into-an-object/156693/3 "2009-01-01T06:49:59Z")

</div>

Thanks Greg! Much nicer than what I threw together.

– Josh  
[http://iammrjoshua.com](http://iammrjoshua.com)

Greg D. wrote:

> On Wed, Dec 31, 2008 at 9:18 PM, Glenn [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > end  
> > x.print\_instance\_variables(:b) # writes the value of @b to the console  
> > x.print\_instance\_variables(:a, :b) # writes the values of @a and @b to the console  
> > x.print\_instance\_variables(:z) # causes an exception because there is no@z
> > 
> > Any suggestions would be greatly appreciated.
> 
> class SomeClass  
> def initialize  
> @a = 1  
> @b = ‘xxx’  
> end
> 
> def print\_instance\_variables( \*args )  
> args.each do |arg|  
> raise unless instance\_variable\_defined?( “@#{ arg }” )  
> puts self.instance\_variable\_get( “@#{ arg }” )  
> end  
> end
> 
> end
> 
> x = SomeClass.new  
> x.print\_instance\_variables( :a )  
> x.print\_instance\_variables( :b )  
> x.print\_instance\_variables( :a, :b )  
> x.print\_instance\_variables( :z )

---

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [January 1, 2009, 5:59am UTC](https://www.ruby-forum.com/t/how-do-i-pass-the-name-of-an-instance-variable-into-an-object/156693/4 "2009-01-01T05:59:35Z")

</div>

On Wed, Dec 31, 2008 at 9:18 PM, Glenn [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end  
> x.print\_instance\_variables(:b) # writes the value of @b to the console  
> x.print\_instance\_variables(:a, :b) # writes the values of @a and @b to the console  
> x.print\_instance\_variables(:z) # causes an exception because there is no@z
> 
> Any suggestions would be greatly appreciated.

class SomeClass  
def initialize  
@a = 1  
@b = ‘xxx’  
end

def print\_instance\_variables( \*args )  
args.each do |arg|  
raise unless instance\_variable\_defined?( “@#{ arg }” )  
puts self.instance\_variable\_get( “@#{ arg }” )  
end  
end

end

x = SomeClass.new  
x.print\_instance\_variables( :a )  
x.print\_instance\_variables( :b )  
x.print\_instance\_variables( :a, :b )  
x.print\_instance\_variables( :z )

---

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [January 1, 2009, 3:25pm UTC](https://www.ruby-forum.com/t/how-do-i-pass-the-name-of-an-instance-variable-into-an-object/156693/5 "2009-01-01T15:25:34Z")

</div>

Hi,

Thanks for your help!

I have a followup question. Can anyone tell me how I could create a  
method that would allow me to add instance variables to an object?

The method would look something like this:

def add\_instance\_variable(x, y)

# code goes here

end

In this example, x would be what I’d like to call the variable and y  
would be the value of the new variable.

Thanks again,

Glenn

* * *

From: Greg D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: ruby-talk ML [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Wednesday, December 31, 2008 11:58:11 PM  
Subject: Re: How do I pass the name of an instance variable into an  
object?

On Wed, Dec 31, 2008 at 9:18 PM, Glenn [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end  
> x.print\_instance\_variables(:b) # writes the value of @b to the console  
> x.print\_instance\_variables(:a, :b) # writes the values of @a and @b to the console  
> x.print\_instance\_variables(:z) # causes an exception because there is no@z
> 
> Any suggestions would be greatly appreciated.

class SomeClass  
def initialize  
@a = 1  
@b = ‘xxx’  
end

def print\_instance\_variables( \*args )  
args.each do |arg|  
raise unless instance\_variable\_defined?( “@#{ arg }” )  
puts self.instance\_variable\_get( “@#{ arg }” )  
end  
end

end

x = SomeClass.new  
x.print\_instance\_variables( :a )  
x.print\_instance\_variables( :b )  
x.print\_instance\_variables( :a, :b )  
x.print\_instance\_variables( :z )

---

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [January 1, 2009, 6:48pm UTC](https://www.ruby-forum.com/t/how-do-i-pass-the-name-of-an-instance-variable-into-an-object/156693/6 "2009-01-01T18:48:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [January 1, 2009, 4:06pm UTC](https://www.ruby-forum.com/t/how-do-i-pass-the-name-of-an-instance-variable-into-an-object/156693/7 "2009-01-01T16:06:42Z")

</div>

Glenn wrote:

> Can anyone tell me how I could create a method that would allow me to add  
> instance variables to an object?

object.instance\_variable\_set(:@variable\_name, “value”)

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [January 1, 2009, 8:09pm UTC](https://www.ruby-forum.com/t/how-do-i-pass-the-name-of-an-instance-variable-into-an-object/156693/8 "2009-01-01T20:09:00Z")

</div>

Glenn wrote:

> I think that this is because of the quotes around initial\_value in this  
> line:  
> self.instance\_variable\_set("@#{name}", “#{initial\_value}”)  
> […]  
> Do you think that there is any harm in leaving those quotes off?

Not at all. The only reason for them to be there in the first place  
would be  
to convert the value to a string (though initial\_value.to\_s would be a  
more  
readable way to do the same thing, in my opinion). Since you don’t want  
that,  
there is no downside to removing them.

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [January 1, 2009, 7:50pm UTC](https://www.ruby-forum.com/t/how-do-i-pass-the-name-of-an-instance-variable-into-an-object/156693/9 "2009-01-01T19:50:15Z")

</div>

Excellent! Thanks so much for your help.

I noticed that the class of the variable always seems to be String when  
I use add\_instance\_variable. When I use instance\_variable\_set, though  
it seems to preserve the class of the object in thee 2nd parameter.

In this case, x is a Fixnum:

s = SomeClass.new  
s.instance\_variable\_set(‘@x’, 22)

In this case, though, x is a String:

s = SomeClass.new  
s.add\_instance\_variable(:x, 22)

I think that this is because of the quotes around initial\_value in this  
line:

self.instance\_variable\_set(“@#{name}”, “#{initial\_value}”)

When I rewrote the line as:

self.instance\_variable\_set(“@#{name}”, initial\_value )

It maintains the original class of that 2nd parameter. Do you think  
that there is any harm in leaving those quotes off?

Glenn

* * *

From: Suman G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: ruby-talk ML [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Thursday, January 1, 2009 12:46:51 PM  
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
