# Macros and macro expansion

**URL:** <https://www.ruby-forum.com/t/macros-and-macro-expansion/95671>\
**Category:** Ruby\
**Created:** [June 8, 2006, 4:53pm UTC](https://www.ruby-forum.com/t/macros-and-macro-expansion/95671 "2006-06-08T16:53:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![countbrass](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9fc29f/32.png) [@countbrass](https://www.ruby-forum.com/u/countbrass)\
**Post date:** [June 8, 2006, 4:53pm UTC](https://www.ruby-forum.com/t/macros-and-macro-expansion/95671/1 "2006-06-08T16:53:25Z")

</div>

I was looking at my controller code (Rails) and the  
edit/create/update/new for  
each are practically identical. So first I DRY’d the four methods in  
each controller to just one and then I decided to write a single macro  
for all my controllers.

This is the macro:

def self.edit\_action\_for(model, options = {})  
model\_class = Object.const\_get(model)  
define\_method(:edit) do  
edit\_code = lambda do  
@thing = model\_class.find\_by\_id(params[:id]) || model\_class.new  
if request.post?  
@thing.attributes = params[:impactable\_area]  
if @thing.save  
flash[:notice] = “#{model.humanize} successfully saved.”  
redirect\_to :action =\> ‘list’  
end  
end  
end  
end  
end

Then I expected this

edit\_action\_for :ImpactableArea

to create my “edit” method.

Problem is that it doesn’t ☹

So then I looked to see how I could do a macro expansion (as you can in  
Lisp) to see what’s being produced and I couldn’t find anything…

HELP!

Edward

---

<div class="post-metadata">

**Author:** ![countbrass](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9fc29f/32.png) [@countbrass](https://www.ruby-forum.com/u/countbrass)\
**Post date:** [June 8, 2006, 6:41pm UTC](https://www.ruby-forum.com/t/macros-and-macro-expansion/95671/2 "2006-06-08T18:41:44Z")

</div>

Hi!

On 6/8/06, Edward K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> define_method(:edit) do
> end
> 
> ```
> 
> So then I looked to see how I could do a macro expansion (as you can in  
> Lisp) to see what’s being produced and I couldn’t find anything…
> 
> HELP!
> 
> Edward
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Your code should work fine if you remove the lammbda from inside the  
define\_method block.

---

<div class="post-metadata">

**Author:** ![countbrass](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9fc29f/32.png) [@countbrass](https://www.ruby-forum.com/u/countbrass)\
**Post date:** [June 8, 2006, 7:45pm UTC](https://www.ruby-forum.com/t/macros-and-macro-expansion/95671/3 "2006-06-08T19:45:39Z")

</div>

ezmobius mob wrote:

> Your code should work fine if you remove the lammbda from inside the  
> define\_method block.

Perfect! Thank you.

(Is there a way to see the results of a macro expansion?)

---

<div class="post-metadata">

**Author:** ![countbrass](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9fc29f/32.png) [@countbrass](https://www.ruby-forum.com/u/countbrass)\
**Post date:** [June 8, 2006, 7:55pm UTC](https://www.ruby-forum.com/t/macros-and-macro-expansion/95671/4 "2006-06-08T19:55:27Z")

</div>

On 6/8/06, Edward K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Unfortunately there is no easy way to see the expanded method. If  
you really want to see it you can use parse\_tree and ruby2c but its  
not a very complete solution

---

<div class="post-metadata">

**Author:** ![countbrass](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9fc29f/32.png) [@countbrass](https://www.ruby-forum.com/u/countbrass)\
**Post date:** [June 8, 2006, 9:07pm UTC](https://www.ruby-forum.com/t/macros-and-macro-expansion/95671/5 "2006-06-08T21:07:53Z")

</div>

ezmobius mob wrote:

> On 6/8/06, Edward K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > –  
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> Unfortunately there is no easy way to see the expanded method. If  
> you really want to see it you can use parse\_tree and ruby2c but its  
> not a very complete solution

Ok thanks for your help 🙂
