# Add dynamic instance methods in a controller

**URL:** <https://www.ruby-forum.com/t/add-dynamic-instance-methods-in-a-controller/76559>\
**Category:** Rails\
**Created:** [November 20, 2006, 2:46pm UTC](https://www.ruby-forum.com/t/add-dynamic-instance-methods-in-a-controller/76559 "2006-11-20T14:46:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![luc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9dc877/32.png) [@luc](https://www.ruby-forum.com/u/luc)\
**Post date:** [November 20, 2006, 2:46pm UTC](https://www.ruby-forum.com/t/add-dynamic-instance-methods-in-a-controller/76559/1 "2006-11-20T14:46:26Z")

</div>

Hello All,

I have a problem while developping an application using rails.  
I’d like to add some methods to my controller depending upon some  
parameters read from an xml file during initialisation (I hope I am  
quite clear 🙂 )  
Below is the code I am using:

* * *

require ‘rexml/Document’  
include REXML

class MyController \< ApplicationController

```
   def index
           # Xsd is a model (not ActiveRecord that is used to get

```

values from an XML file  
xsd = Xsd.new(“myfile.xsd”)

```
           # Get main section
           @main_sections = xsd.get_main_sections

           # Get item for first section
           @item =

```

xsd.get\_items\_per\_section(@main\_sections[0].to\_s)

```
           # Dynamically create method for to retrieve list of

```

## fields for each main sections # THIS PART IS NOT WORKING @main\_sections.each do |section| self.instance\_eval{ define\_method(“get\_item\_for\_#{section}”) {“toto”}} end end end

While testing this, I have an error message saying that the  
define\_method does not exist !!!  
Why is this method not found ???  
I am still not sure about the instance\_eval, class\_eval, define\_method  
I need to use.  
Do you have any clues regarding this problem ?

Thanks a lot,

Luc

---

<div class="post-metadata">

**Author:** ![luc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9dc877/32.png) [@luc](https://www.ruby-forum.com/u/luc)\
**Post date:** [October 25, 2007, 5:02pm UTC](https://www.ruby-forum.com/t/add-dynamic-instance-methods-in-a-controller/76559/2 "2007-10-25T17:02:32Z")

</div>

Hi Luc,

I am facing the same problem loading the data from YAML and using AR to  
store metrics only and final data.

I am still looking for a solution to dynamically create attributes but i  
am not that the controller is the place to do that. I believe it should  
be done on the model level.

Did you think about the possibility of creating a class and load the  
methods into it?

Try this link to learn more about metaprogramming:

> **[Ruby Metaprogramming techniques](http://ola-bini.blogspot.com/2006/09/ruby-metaprogramming-techniques.html)**
>
> Updated: Scott Labounty wondered how the trace example could work and since a typical metaprogramming technique is writing before- and after...

Cheers,

Luc wrote:

> Hello All,
> 
> I have a problem while developping an application using rails.  
> I’d like to add some methods to my controller depending upon some  
> parameters read from an xml file during initialisation (I hope I am  
> quite clear 🙂 )  
> Below is the code I am using:
> 
> * * *
> 
> require ‘rexml/Document’  
> include REXML
> 
> class MyController \< ApplicationController
> 
> ```
> def index
> # Xsd is a model (not ActiveRecord that is used to get
> 
> ```
> 
> values from an XML file  
> xsd = Xsd.new(“myfile.xsd”)
> 
> ```
> # Get main section
> @main_sections = xsd.get_main_sections
> 
> # Get item for first section
> @item =
> 
> ```
> 
> xsd.get\_items\_per\_section(@main\_sections[0].to\_s)
> 
> ```
> # Dynamically create method for to retrieve list of
> 
> ```
> 
> ## fields for each main sections # THIS PART IS NOT WORKING @main\_sections.each do |section| self.instance\_eval{ define\_method(“get\_item\_for\_#{section}”) {“toto”}} end end end
> 
> While testing this, I have an error message saying that the  
> define\_method does not exist !!!  
> Why is this method not found ???  
> I am still not sure about the instance\_eval, class\_eval, define\_method  
> I need to use.  
> Do you have any clues regarding this problem ?
> 
> Thanks a lot,
> 
> Luc
