在 Ruby 中,如何获取对包含用于调用的参数的方法的引用?
给出这段代码:
a = {1=>2}
m = a.method(:[])
我知道我现在可以使用 :
value = m.call(1)
,它将返回 2。问题是,我需要更改什么,以便我可以像 : 一样直接调用该方法,
m.call()
并且它将得到 1 作为参数发送?如果能够编写如下内容那就太好了:
m = a.method(:[],1) # where the symbol is the method, and 1 will be the parameter it will be called with
问题是,我想延迟执行脚本的某些部分,直到创建一些对象,并且我想避免重写所有内容以使用 lambda。
Given this code:
a = {1=>2}
m = a.method(:[])
I know that I can now use :
value = m.call(1)
and it will return 2. The thing is, what do I need to change so that I can call the method directly like :
m.call()
and it will get the 1 sent as a parameter? It would be nice to be able to write something like :
m = a.method(:[],1) # where the symbol is the method, and 1 will be the parameter it will be called with
The thing is, I'd like to delay the execution of certain parts of my script until some objects get created, and I'd like to avoid rewriting EVERYTHING to use lambdas.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
基本上,您想要的是一种柯里化函数的方法。
http://en.wikipedia.org/wiki/Curry_function
这可以通过许多不同的方式完成方式之一:
您可以将其添加到 Hash 的元类中,或者添加到您想要包含在某些对象中的模块中,等等。然后,调用它就成为您想要的用例:
Basically, what you want is a way to curry the function.
http://en.wikipedia.org/wiki/Curry_function
This can be done in many different ways, one of which:
You can add this to Hash's metaclass, or to a module you want to include in some of your objects, etc. Then, calling it becomes the usecase you wanted:
我确信,有不止一种方法可以做到这一点。
There's more than one way to do it, I'm sure.
听起来您应该将方法参数附加到您正在调用该方法的对象,并让该方法将它们作为实例变量访问。
就简单的重构步骤而言:
例如,重构调用代码如下:
像这样工作:
它并不性感或聪明,但它会产生表达其意图的代码,并且很有可能它会解决真正的问题,即您的代码中缺少某些东西。模型。
It sounds like you should attach the method parameters to the object you're calling the method on, and have the method access them as instance variables.
In terms of simple refactoring steps:
As an example, refactor calling code like this:
to work like this:
It's not sexy or clever, but it will result in code that expresses its intent, and odds are good that it will address the real problem, namely that something is missing from your model.