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
Here we invoke send() on the module Accessors, passing
the :define_method symbol, which represents the define_method private
method available from Class class. We create an instance method called
default_locale, whose value is the block passed from register_detail
class method of ActionView::LookupContext. Note that we later use
"include Accessors" within LookupContext class to make that available
as an instance method of LookupContext. We then create more instance
methods using module_eval, with here-documents to specify our
delimiter (METHOD) and the dynamic constants (__FILE__, __LINE__) for
accurate error reporting on stacktrace. 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?
on 2012-09-11 19:10
on 2012-09-11 23:14
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 &block?
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.