繁体   English   中英

Ruby Metaprogramming方法列表?

[英]List of Ruby Metaprogramming methods?

刚开始学习Ruby元编程。 看看Object.methods我得到:

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_evalinitializemethod_missing

下面是从顶端回答这个页面:

方法相关的钩子

method_missing
method_added
singleton_method_added
method_removed
singleton_method_removed
method_undefined
singleton_method_undefined

类和模块挂钩

inherited
append_features
included
extend_object
extended
initialize_copy
const_missing

编组挂钩

marshal_dump
marshal_load

强制钩子

coerce
induced_from
to_xxx

另外,请查看博客文章,了解其中许多方法的说明和示例代码。

Object是一个类,因此您列出的大多数方法实际上是Class实例方法。 再看看Object.private_methods - 你会在那里找到define_method ,这绝对是必不可少的。 但是binding也非常强大......在学习Ruby元编程时,你会想看看Binding类的文档。 send__send__public_send系列也很重要。

查看上面的列表,您应该能够识别“反射”方法,这些方法可用于以编程方式查询和操作常量,方法,实例变量等。 然后是evalexec方法。 method_missing是你学习的第一个,但也要注意const_missing 请务必查看set_trace_functrace_var 我们在哪里没有aliasalias_method

然后是解释器“钩子”,如method_addedmethod_removedmethod_undefinedinheritedextendedincludedsingleton_method_addedsingleton_method_removedsingleton_method_undefinedinstance_method_addedinstance_method_removedinstance_method_undefined

Object#method对于获取Method对象至关重要。 查看Method所有Method ,包括owner东西。 Kernel#caller有时很有用。 然后还查找ObjectSpace类。

理解虽然它们有一些特殊的行为,但类和模块只是对象,您可以动态创建它们,将它们存储在数据结构中等等。它们甚至不必具有名称。 你可以调用Class.new来创建一个新的,并根据需要使用class_evaldefine_method等来为它添加方法。

__LINE____FILE__可能很有趣,特别是File.read(__FILE__)

理解块和lambdas对于一般的Ruby编程和特别是元编程都很重要。

在“Metaprogramming Ruby”一书中,作者指出元编程和编程之间没有确定的界限。 这一切都只是编程。

我可以想到的一些例子是编程和元编程之间的classattr_reader

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM