Tuesday, September 11, 2012

[Rails] Re: define_method vs module_eval

John Merlino wrote in post #1075500:
> Rails code:
>
> Accessors.send :define_method, :"default_#{name}", &block
>
> Accessors.module_eval <<-METHOD, __FILE__, __LINE__ + 1
> def #{name}
> @details.fetch(:#{name}, [])
> end
>
> def #{name}=(value)
> value = value.present? ? Array(value) : default_#{name}
> _set_detail(:#{name}, value) if value != @details[:#{name}]
> end
>
> remove_possible_method :initialize_details
> def initialize_details(details)
> #{initialize.join("\n")}
> end
> METHOD
> end
>
> My question is why not include
> default_#{name} as part of the evaluated string in module_eval, rather
> than invoking send() directly above that using define_method?
>

The variation of module_eval() used in the example takes a String as an
argument. So "default_#{name}" could certainly be inserted in the
String argument because it is a String, but what about the other
argument: &block?

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate