Forum: Ruby Macros and macro expansion

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
77753dfbc8b973886f745712555aee6b?d=identicon&s=25 Edward Kenworthy (Guest)
on 2006-06-08 16:53
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
6076c22b65b36f5d75c30bdcfb2fda85?d=identicon&s=25 ezmobius mob (Guest)
on 2006-06-08 18:41
(Received via mailing list)
Hi!

On 6/8/06, Edward Kenworthy <edward@kenworthy.info> 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/.
>
>

Your code should work fine if you remove the lammbda from inside the
define_method block.
77753dfbc8b973886f745712555aee6b?d=identicon&s=25 Edward Kenworthy (Guest)
on 2006-06-08 19:45
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?)
6076c22b65b36f5d75c30bdcfb2fda85?d=identicon&s=25 ezmobius mob (Guest)
on 2006-06-08 19:55
(Received via mailing list)
On 6/8/06, Edward Kenworthy <edward@kenworthy.info> wrote:
> --
> Posted via 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
77753dfbc8b973886f745712555aee6b?d=identicon&s=25 Edward Kenworthy (Guest)
on 2006-06-08 21:07
ezmobius mob wrote:
> On 6/8/06, Edward Kenworthy <edward@kenworthy.info> wrote:
>> --
>> Posted via 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 :-)
This topic is locked and can not be replied to.