# Newbie ..need help with metaprogramming

**URL:** <https://www.ruby-forum.com/t/newbie-need-help-with-metaprogramming/178886>\
**Category:** Ruby\
**Created:** [November 13, 2009, 1:11am UTC](https://www.ruby-forum.com/t/newbie-need-help-with-metaprogramming/178886 "2009-11-13T01:11:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dhaval](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/85e7bf/32.png) [@dhaval](https://www.ruby-forum.com/u/dhaval)\
**Post date:** [November 13, 2009, 1:11am UTC](https://www.ruby-forum.com/t/newbie-need-help-with-metaprogramming/178886/1 "2009-11-13T01:11:06Z")

</div>

hi, i wrote a small code to check metaprogramming in ruby…what am i  
doing wrong…can you pl help?

class Testvar  
attr\_accessor :a1  
def initialize  
@a1=‘init’  
end  
end

var =":a1"

t = Testvar.new  
if t.respond\_to?("#{var}")  
p ‘test 1- pass’  
end  
if t.instance\_variable\_set var , ‘abc’  
p ‘test 2-pass’  
end  
if t.send var , ‘def’  
p ‘test 3-pass’  
end

---

<div class="post-metadata">

**Author:** ![dhaval](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/85e7bf/32.png) [@dhaval](https://www.ruby-forum.com/u/dhaval)\
**Post date:** [November 13, 2009, 1:43am UTC](https://www.ruby-forum.com/t/newbie-need-help-with-metaprogramming/178886/2 "2009-11-13T01:43:18Z")

</div>

On 11/12/09, Dhaval [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> var =“:a1”  
> end  
> What did you expect it to do, and what did it actually do?  
> I’m guessing that it blows up at instance\_variable\_set, because a1 is  
> not a valid ivar name. Try with an @ instead:

t.instance\_variable\_set(“@#{var}”, ‘abc’)

---

<div class="post-metadata">

**Author:** ![dhaval](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/85e7bf/32.png) [@dhaval](https://www.ruby-forum.com/u/dhaval)\
**Post date:** [November 13, 2009, 1:45am UTC](https://www.ruby-forum.com/t/newbie-need-help-with-metaprogramming/178886/3 "2009-11-13T01:45:38Z")

</div>

On Thu, Nov 12, 2009 at 6:05 PM, Dhaval [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> var =“:a1”  
> end

Try like this:

class Testvar  
attr\_accessor :a1  
def initialize  
@a1=‘init’  
end  
end

var = :a1

t = Testvar.new

if t.respond\_to?(var)  
puts ‘test 1- pass’  
end

t.instance\_variable\_set “@#{var}” , ‘abc’  
if t.instance\_variable\_get(“@#{var}”) == ‘abc’  
puts ‘test 2-pass’  
end

t.send “#{var}=” , ‘def’  
if t.send(var) == ‘def’  
puts ‘test 3-pass’  
end

---

<div class="post-metadata">

**Author:** ![dhaval](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/85e7bf/32.png) [@dhaval](https://www.ruby-forum.com/u/dhaval)\
**Post date:** [November 14, 2009, 12:58am UTC](https://www.ruby-forum.com/t/newbie-need-help-with-metaprogramming/178886/4 "2009-11-14T00:58:52Z")

</div>

> > if t.respond\_to?("#{var}")

Supernewbie question: Where is the documentation for respond\_to? ?

Obviously … I am just beginning to learn Ruby after trying to absorb  
the Ruby on Rails and PickAxe books.

---

<div class="post-metadata">

**Author:** ![dhaval](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/85e7bf/32.png) [@dhaval](https://www.ruby-forum.com/u/dhaval)\
**Post date:** [November 14, 2009, 2:01am UTC](https://www.ruby-forum.com/t/newbie-need-help-with-metaprogramming/178886/5 "2009-11-14T02:01:29Z")

</div>

On Fri, Nov 13, 2009 at 5:58 PM, Ralph S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > > if t.respond\_to?(“#{var}”)
> 
> Supernewbie question: Where is the documentation for respond\_to? ?
> 
> Obviously … I am just beginning to learn Ruby after trying to absorb  
> the Ruby on Rails and PickAxe books.

For 1.8.6:  
[http://ruby-doc.org/core/](http://ruby-doc.org/core/)

Then search for “respond\_to?” and you will see three classes which  
define  
it, in the frame on the far right, which lists methods. The three you  
will  
see are:

respond\_to?  
(DRb::DRbObject)[http://ruby-doc.org/core/classes/DRb/DRbObject.html#M007246](http://ruby-doc.org/core/classes/DRb/DRbObject.html#M007246)  
respond\_to? (Object)  
[http://ruby-doc.org/core/classes/Object.html#M000331](http://ruby-doc.org/core/classes/Object.html#M000331)  
respond\_to?  
(Delegator)[http://ruby-doc.org/core/classes/Delegator.html#M002698](http://ruby-doc.org/core/classes/Delegator.html#M002698)

Click the one for Object, and it should take you to  
[http://ruby-doc.org/core/classes/Object.html#M000331](http://ruby-doc.org/core/classes/Object.html#M000331)

---

<div class="post-metadata">

**Author:** ![dhaval](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/85e7bf/32.png) [@dhaval](https://www.ruby-forum.com/u/dhaval)\
**Post date:** [November 16, 2009, 11:46am UTC](https://www.ruby-forum.com/t/newbie-need-help-with-metaprogramming/178886/6 "2009-11-16T11:46:04Z")

</div>

> var =":a1"
> 
> t = Testvar.new  
> if t.respond\_to?("#{var}")  
> p ‘test 1- pass’  
> end

Here you are testing if your object has a particular method. The method  
name is “a1”. You can either pass the string “a1” or the symbol :a1

However you passed the string “:a1” consisting of three characters -  
colon, a, 1.

> if t.instance\_variable\_set var , ‘abc’  
> p ‘test 2-pass’  
> end

Here you are talking about an instance variable, not a method. The  
instance variable’s name is “@a1”. You can either pass the string “@a1”  
or the symbol :@a1

> if t.send var , ‘def’  
> p ‘test 3-pass’  
> end

Here you’re trying to call a setter method. The method’s name is “a1=”.  
Again, you can either pass the string “a1=” or the symbol :a1=
