- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我试图找到一种方法来在 Ruby (1.8) 的 method_missing 中从调用者那里获取绑定(bind),但我似乎无法找到实现它的方法。
希望下面的代码能解释我想做什么:
class A
def some_method
x = 123
nonexistent_method
end
def method_missing(method, *args, &block)
b = caller_binding # <---- Is this possible?
eval "puts x", b
end
end
A.new.some_method
# expected output:
# 123
那么...有没有办法获得调用者的绑定(bind),或者这在 Ruby (1.8) 中是不可能的?
最佳答案
这是一个(有点脆弱的)hack:
# caller_binding.rb
TRACE_STACK = []
VERSION_OFFSET = { "1.8.6" => -3, "1.9.1" => -2 }[RUBY_VERSION]
def caller_binding(skip=1)
TRACE_STACK[ VERSION_OFFSET - skip ][:binding]
end
set_trace_func(lambda do |event, file, line, id, binding, classname|
item = {:event=>event,:file=>file,:line=>line,:id=>id,:binding=>binding,:classname=>classname}
#p item
case(event)
when 'line'
TRACE_STACK.push(item) if TRACE_STACK.empty?
when /\b(?:(?:c-)?call|class)\b/
TRACE_STACK.push(item)
when /\b(?:(?:c-)?return|end|raise)\b/
TRACE_STACK.pop
end
end)
这适用于您的示例,但我还没有对它进行太多测试
require 'caller_binding'
class A
def some_method
x = 123
nonexistent_method
end
def method_missing( method, *args, &block )
b = caller_binding
eval "puts x", b
end
end
x = 456
A.new.some_method #=> prints 123
A.new.nonexistent_method #=> prints 456
当然,如果绑定(bind)没有定义您要评估的变量,这将不起作用,但这是绑定(bind)的普遍问题。如果 undefined variable ,则它不知道它是什么。
require 'caller_binding'
def show_x(b)
begin
eval <<-SCRIPT, b
puts "x = \#{x}"
SCRIPT
rescue => e
puts e
end
end
def y
show_x(caller_binding)
end
def ex1
y #=> prints "undefined local variable or method `x' for main:Object"
show_x(binding) #=> prints "undefined local variable or method `x' for main:Object"
end
def ex2
x = 123
y #+> prints "x = 123"
show_x(binding) #+> prints "x = 123"
end
ex1
ex2
要解决这个问题,您需要在评估的字符串中进行一些错误处理:
require 'caller_binding'
def show_x(b)
begin
eval <<-SCRIPT, b
if defined? x
puts "x = \#{x}"
else
puts "x not defined"
end
SCRIPT
rescue => e
puts e
end
end
def y
show_x(caller_binding)
end
def ex1
y #=> prints "x not defined"
show_x(binding) #=> prints "x not defined"
end
def ex2
x = 123
y #+> prints "x = 123"
show_x(binding) #+> prints "x = 123"
end
ex1
ex2
关于ruby - 如何从 method_missing 获取绑定(bind)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1314592/
我正在尝试实现 method_missing 以将 $ 转换为其他货币,如 5.dollars yield 为 5、5.yen 将 yield 为 0.065 5.euro 6.56 等等。我现在可以
我有 Java 和 Ruby 背景,我想知道在 Erlang 中是否有任何等同于“method_missing”的东西。我查看了文档,我能看到的最接近的是使用 erl_eval 和 erl_parse
我正在尝试实现一个代码,在 method_missing 的帮助下,如果它符合某个关键字,它会自动创建一个方法。 到目前为止这有效: class Add def initialize()
我的程序中有一个团队类,我正在尝试使用 method_missing但是当该方法不存在时它没有运行该函数,而是给我一个错误:“团队的未定义方法‘hawks’(NoMethodError)” 我的代码如
我有一个 Wrapper 类,它支持添加您可以稍后查找的选项。它将这些选项存储在内部散列 @dict 中。 w = Wrapper.new w.foo # => NameError w.foo = 1
当我的 ruby 脚本失败时,我正在构建一种通过 mutt 向我发送电子邮件的方法。它看起来像这样: begin UnknownFunction() rescue subject
我正在尝试使用 method_missing 将美元转换为不同的货币。 class Numeric @@currency={"euro"=>2, "yen"=>6} def metho
我有以下 Ruby 方法,它给出了 RSpec 错误“堆栈级别太深”,我不确定为什么 - 非常感谢任何帮助! def method_missing(method_name, *args) full
在下面的代码中,roar 方法没有定义在 Lion 类中,但仍然可以使用 method_missing 调用。 class Lion def method_missing(name, *args)
我刚开始学习 Rails,我无法理解: 在我的 Post Controller 中,我没有显示方法(未描述),但我在我的 Controller 中放入了: def method_missing(nam
我是 Ruby 的新手,现在想了解一些关于元编程的知识。我想返回错过的方法名: class Numeric attr_accessor :method_name def method_miss
class Numeric @@currencies = {'yen' => 0.013, 'euro' => 1.292, 'rupee' => 0.019, 'dollar' => 1.0}
我知道 method_missing 是 Ruby 处理消息时的最后手段。我的理解是,它在对象层次结构中向上寻找与符号匹配的已声明方法,然后向下寻找最低的已声明method_missing。这比标准方
在方法定义中,有没有办法判断方法 method_missing 是被显式调用还是作为钩子(Hook)方法被调用? 使用方法initialize,可以通过执行以下操作来判断它是被显式调用还是作为钩子(H
我写了一个方便的 ActiveRecord 扩展来将方法委托(delegate)给一个基础对象(基于 multi-table inheritance) class ActiveRecord::Base
我有一个 ruby 程序,我想接受用户创建的方法,并使用该名称创建一个新方法。我试过这个: def method_missing(meth,*args,&block) name = meth.
我正在尝试编写一个通用模块,以将用于动态方法创建的 method_missing 模式应用于我的某些 Rails 模型。这些模型既有类方法也有实例方法。虽然我可以相当直接地为任一类案例编写模块:
类似于 Ruby 中的那个 最佳答案 是 ,从 Scala 2.9 开始,带有 -Xexperimental选项,可以使用 Dynamic特征 ( scaladoc )。扩展 Dynamic 的类获取
我一直在研究 powershell 的动态能力,我想知道一些事情 powershell 中是否有类似于 Ruby 的 method_missing() 的东西,您可以在其中设置“捕获所有方法”以动态处
我想为我的一个类定义一个 method_missing 函数,并且我希望能够传入一个散列作为参数列表而不是数组。像这样: MyClass::get_by_id {:id => id} MyClass:
我是一名优秀的程序员,十分优秀!