访问 self.method_added 中的变量名
我相信在 Ruby 中,有一种方法可以访问块中所有局部变量的名称。
def some_method(param1, param2)
p local_variables
end
每当调用'some_method'时,param1和param2都会被打印出来。不是价值!但变量名。
现在,我想在 self.method_added 内实现相同的结果。
每当定义一个方法时,都会调用 self.method_added 。我希望能够访问 self.method_added 中定义的方法的局部变量的名称。例如,
def self.method_added(method_name)
#prints the variables names of the argument for method method_name
end
def do_something param1, param2
#crazy stuff
end
使用上面的代码,当创建 do_something 时,我想访问变量名“param1”和“param2”
有没有办法做到这一点?
I believe in Ruby, there is a way to access the name of all local variables within a block.
def some_method(param1, param2)
p local_variables
end
whenever 'some_method' is called, param1, and param2 will be printed out. Not the value! but the variable names.
Now, I would like to achieve the same result but within the self.method_added.
Whenever a method is defined, self.method_added is called. I want to be able to access the names of the local variables of the method being defined inside self.method_added. For example,
def self.method_added(method_name)
#prints the variables names of the argument for method method_name
end
def do_something param1, param2
#crazy stuff
end
with the code above, when do_something is created, I would like to have access to the variable name 'param1' and 'param2'
Is there a way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据您的 ruby 版本,您可能会考虑 ruby2ruby。
请参阅: http://www.slideshare.net/marc_chung/RubyConfRubyDosRuby
它允许您获得代码的 AST(抽象语法树)。但上次我检查它只适用于 1.8。
Depending on your ruby version you might consider ruby2ruby.
See: http://www.slideshare.net/marc_chung/RubyConfRubyDosRuby
It allows you to get an AST (abstract syntax tree) of your code. But last time I checked it worked only with 1.8.