# How to create dinamicly method

**URL:** <https://www.ruby-forum.com/t/how-to-create-dinamicly-method/88907>\
**Category:** Ruby\
**Created:** [March 15, 2007, 2:57pm UTC](https://www.ruby-forum.com/t/how-to-create-dinamicly-method/88907 "2007-03-15T14:57:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ther](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/977dab/32.png) [@ther](https://www.ruby-forum.com/u/ther)\
**Post date:** [March 15, 2007, 2:57pm UTC](https://www.ruby-forum.com/t/how-to-create-dinamicly-method/88907/1 "2007-03-15T14:57:52Z")

</div>

What I am trying to accomplist is:

class MyClass  
def addVar(var, val)  
instance\_variable\_set("@#{var}", val)  
define\_method("#{var}") { || instance\_variable\_get("#{var}") }  
end  
end

c = MyClass.new  
c.addVar(‘aa’,10) # here pops the error  
c.aa # should return 10

But I get this error:  
NoMethodError: undefined method `define_method' for #<MyClass:0x2d01cc0 @aa=10> from (irb):4:in`addVar’  
from (irb):8

I know this should work because it’s used all over rails.

Advice me please

TheR

---

<div class="post-metadata">

**Author:** ![ther](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/977dab/32.png) [@ther](https://www.ruby-forum.com/u/ther)\
**Post date:** [March 15, 2007, 3:07pm UTC](https://www.ruby-forum.com/t/how-to-create-dinamicly-method/88907/2 "2007-03-15T15:07:22Z")

</div>

On Thursday 15 March 2007 13:57, Damjan R. wrote:

> c.addVar(‘aa’,10) # here pops the error  
> Advice me please
> 
> TheR  
> define\_method won’t work in instance method, you have to use class  
> method o  
> just use eval(“def #{var}; here return value; end”);

---

<div class="post-metadata">

**Author:** ![ther](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/977dab/32.png) [@ther](https://www.ruby-forum.com/u/ther)\
**Post date:** [March 15, 2007, 3:17pm UTC](https://www.ruby-forum.com/t/how-to-create-dinamicly-method/88907/3 "2007-03-15T15:17:38Z")

</div>

Marcin R. wrote:

> On Thursday 15 March 2007 13:57, Damjan R. wrote:
> 
> > c.addVar(‘aa’,10) # here pops the error  
> > Advice me please
> > 
> > TheR  
> > define\_method won’t work in instance method, you have to use class  
> > method o  
> > just use eval(“def #{var}; here return value; end”);

Yep: Each of these two variants work:  
eval “def #{var};instance\_variable\_get(’@#{var}’);end”  
eval(“def #{var}; @#{var}; end”)

I was trying this too, but forgot to add @ to variable name.

Thanks

TheR

---

<div class="post-metadata">

**Author:** ![ther](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/977dab/32.png) [@ther](https://www.ruby-forum.com/u/ther)\
**Post date:** [March 16, 2007, 10:47pm UTC](https://www.ruby-forum.com/t/how-to-create-dinamicly-method/88907/4 "2007-03-16T22:47:52Z")

</div>

On Thu, Mar 15, 2007 at 10:57:54PM +0900, Damjan R. wrote:

> What I am trying to accomplist is:
> 
> class MyClass  
> def addVar(var, val)  
> instance\_variable\_set("@#{var}", val)  
> define\_method("#{var}") { || instance\_variable\_get("#{var}") }

```
    self.class.class_eval {
      define_method("#{var}") { || instance_variable_get("@#{var}") 

```

}  
}

> ```
> end
> 
> ```
> 
> end
> 
> c = MyClass.new  
> c.addVar(‘aa’,10) # here pops the error  
> c.aa # should return 10

Regards,

Brian.
