【问题标题】:List of Ruby Metaprogramming methods?Ruby 元编程方法列表?
【发布时间】:2012-03-18 10:46:20
【问题描述】:

刚开始学习 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

【问题讨论】:

标签: ruby object reflection methods metaprogramming


【解决方案1】:

这是来自this页面的最佳答案:

方法相关的钩子

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

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

【讨论】:

  • 请注意,这些都是很棒的钩子,但不包括 instance_evaldefine_method 之类的东西。
【解决方案2】:

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_addedsingleton_method_undefined、@98 @、instance_method_removedinstance_method_undefined

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

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

__LINE____FILE__ 可能很有趣,尤其是File.read(__FILE__)

很好地理解块和 lambda 对于一般的 Ruby 编程,尤其是元编程都很重要。

【讨论】:

    【解决方案3】:

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

    我能想到的一些介于编程和元编程之间的例子是classattr_reader

    【讨论】:

      猜你喜欢
      • 2015-06-03
      • 1970-01-01
      • 1970-01-01
      • 2020-03-01
      • 1970-01-01
      • 2011-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多