剛剛開始學習Ruby元編程。看Object.methods我得到:Ruby元編程方法列表?
Object.methods => [
:allocate,
:new,
:superclass,
:freeze,
:===,
:==,
:<=>,
:<,
:<=,
:>,
:>=,
:to_s,
:included_modules,
:include?,
:name,
:ancestors,
:instance_methods,
:public_instance_methods,
:protected_instance_methods,
:private_instance_methods,
:constants,
:const_get,
:const_set,
:const_defined?,
:const_missing,
:class_variables,
:remove_class_variable,
:class_variable_get,
:class_variable_set,
:class_variable_defined?,
:module_exec,
:class_exec,
:module_eval,
:class_eval,
:method_defined?,
:public_method_defined?,
:private_method_defined?,
:protected_method_defined?,
:public_class_method,
:private_class_method,
:autoload,
:autoload?,
:instance_method,
:public_instance_method,
:nil?,
:=~,
:!~,
:eql?,
:hash,
:class,
:singleton_class,
:clone,
:dup,
:initialize_dup,
:initialize_clone,
:taint,
:tainted?,
:untaint,
:untrust,
:untrusted?,
:trust,
:frozen?,
:inspect,
:methods,
:singleton_methods,
:protected_methods,
:private_methods,
:public_methods,
:instance_variables,
:instance_variable_get,
:instance_variable_set,
:instance_variable_defined?,
:instance_of?,
:kind_of?,
:is_a?,
:tap,
:send,
:public_send,
:respond_to?,
:respond_to_missing?,
:extend,
:display,
:method,
:public_method,
:define_singleton_method,
:__id__,
:object_id,
:to_enum,
:enum_for,
:equal?,
:!,
:!=,
:instance_eval,
:instance_exec,
:__send__]
是否有一個方法的列表是有用的元編程?如instance_eval
,initialize
和method_missing
?
如果元編程Ruby是一個嚴肅的興趣,我強烈推薦Paolo Perrotta編寫的[Metoprogramming Ruby book](http://pragprog.com/book/ppmetr/metaprogramming-ruby)。 – sarnold 2012-02-28 22:34:13