# Help with "generating" code

**URL:** <https://www.ruby-forum.com/t/help-with-generating-code/64385>\
**Category:** Ruby\
**Created:** [June 25, 2006, 7:13am UTC](https://www.ruby-forum.com/t/help-with-generating-code/64385 "2006-06-25T07:13:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![merc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f07891/32.png) [@merc](https://www.ruby-forum.com/u/merc)\
**Post date:** [June 25, 2006, 7:13am UTC](https://www.ruby-forum.com/t/help-with-generating-code/64385/1 "2006-06-25T07:13:16Z")

</div>

Hello,

OK, this is a Ruby question, even though it’s for a RoR project 😃  
I am a pretty hopeless Ruby programmer. I am getting better, but when  
it’s about eval(), symbols and introspection, I still crumble…  
Besides, I am not even sure what I want to do is actually possible in  
Ruby.

At one point, in my program I have this:

def create  
@channel = Channel.new(params[:channel])  
if @channel.save  
flash[:notice] = ‘Channel was successfully created.’  
redirect\_to :action =\> ‘index’  
else  
render :action =\> ‘new’  
end  
end

What I want to do, is being able to write:

def create  
ApplicationController::standard\_create(:channel)  
end

That “standard\_create” function should execute the code, but in  
create()'s scope.  
What I don’t know is:

1. How to keep the code execute by  
ApplicationController::standard\_create in create()'s scope
2. From ApplicationController::standard\_create(name), what to do to  
“create the code”

Any help would be _immensely_ appreciated!

Thanks a lot,

Merc.

---

<div class="post-metadata">

**Author:** ![merc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f07891/32.png) [@merc](https://www.ruby-forum.com/u/merc)\
**Post date:** [June 25, 2006, 4:16pm UTC](https://www.ruby-forum.com/t/help-with-generating-code/64385/2 "2006-06-25T16:16:34Z")

</div>

Merc,

You could add a method to Channel that looks like:  
class Channel  
def self.standard\_create(params, controller)  
ret = Channel.new params  
if ret.save  
controller.flash[:notice] = ‘Channel was saved’  
controller.redirect\_to :action =\> ‘index’  
else  
controller.render :action =\> ‘new’  
end  
return ret  
end  
end

No need for metaprogramming in this case, just a simple method.

Thanks,

David

---

<div class="post-metadata">

**Author:** ![merc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f07891/32.png) [@merc](https://www.ruby-forum.com/u/merc)\
**Post date:** [June 25, 2006, 5:27pm UTC](https://www.ruby-forum.com/t/help-with-generating-code/64385/3 "2006-06-25T17:27:06Z")

</div>

Lightly tested:

class Parent  
def self.standard\_create(name)  
upper\_name = name.to\_s.capitalize  
klass = Kernel::const\_get upper\_name  
define\_method(:create) do  
ret = instance\_variable\_set("@#{name}", klass.new(params[name]))  
if ret.save  
flash[:notice] = “#{upper\_name} successfully created.”  
redirect\_to :action =\> ‘index’  
else  
render :action =\> ‘new’  
end  
end  
end

def flash() (@flash||={}) end  
def params() (@params||={}) end  
def redirect\_to(what) puts “Redirecting to #{what[:action]}” end  
def render(hash) puts “Rendering #{hash[:action]}” end

end

Test2 = Struct.new :params  
class Test2  
def save  
puts “Saving with #{params}”  
end  
end

Only the standard\_create method is important, the result is just fluff  
to emulate some of Rails and show its usage.

---

<div class="post-metadata">

**Author:** ![merc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f07891/32.png) [@merc](https://www.ruby-forum.com/u/merc)\
**Post date:** [June 26, 2006, 3:27am UTC](https://www.ruby-forum.com/t/help-with-generating-code/64385/4 "2006-06-26T03:27:22Z")

</div>

Hi,

_ **THANK YOU** _

I ended up using a solution that was a little different to what I  
originally thought of. However, your input was _vital_ for me. The main  
problem was that I was trying to write a class method, where an instance  
method was probably a better idea.  
Let me show what I did. I know this is RoR code. However, what I am  
focusing on is Ruby code here.  
So, here is what I did.  
In the application controller:

class Lookup::AccountNumberController \< ApplicationController  
[…]

def create  
standard\_create :account\_number  
end  
[…]

Then, in the ApplicationController the real magic happens:

class ApplicationController \< ActionController::Base  
[…]

def standard\_create(name)

```
# Fetch the class's pointer called "Name" - THANK YOU Ruby ML!
klass = self.class::const_get(Inflector.classify(name.to_s))

# Set @name. For example:
# @channel = Channel.new(params[:channel]) - - THANK YOU Ruby ML!
variable=instance_variable_set('@'+name.to_s,klass.new(params[name]) 

```

)

```
# Try and save the object, blah blah
if variable.save
  flash[:notice] = "#{name.to_s.humanize} was successfully created."
  redirect_to :action => 'index' if ! parent
else
  render :action => 'new'
end

```

end  
[…]  
end

The big issue is that I needed to assign an instance variable, but all I  
had was the name. So,  
instance\_variable\_set(’@’+name.to\_s,klass.new(params[name]) ) really  
did the trick. Also, it took me a while to get klass =  
self.class::const\_get(Inflector.classify(name.to\_s)) (OK, it probably  
sounds totally obvious to you…).

That  
variable=instance\_variable\_set(’@’+name.to\_s,klass.new(params[name]) )  
is a bit of an eye sore. I wonder if there’s a better way of “pointing”  
to the instance variable directly…?

Anyway… Ruby truly is beautiful. In my application, I have _LOADS_ and  
_LOADS_ of tables that are ridicolously similar. Well, this techniques  
literally saves me from cutting&pasting the same code over and over  
again.

I will probably end up writing an article about this (when I am more  
comfortable with what I’m doing).

Bye!

Merc.
