ruby 中的条件链接
在 Ruby 中有条件链接方法的好方法吗?
我想要做的功能是
if a && b && c
my_object.some_method_because_of_a.some_method_because_of_b.some_method_because_of_c
elsif a && b && !c
my_object.some_method_because_of_a.some_method_because_of_b
elsif a && !b && c
my_object.some_method_because_of_a.some_method_because_of_c
etc...
根据许多条件我想计算出在方法链中调用哪些方法。
到目前为止,我以“好方法”做到这一点的最佳尝试是有条件地构建方法字符串,并使用 eval
,但肯定有更好、更红宝石的方法吗?
Is there a good way to chain methods conditionally in Ruby?
What I want to do functionally is
if a && b && c
my_object.some_method_because_of_a.some_method_because_of_b.some_method_because_of_c
elsif a && b && !c
my_object.some_method_because_of_a.some_method_because_of_b
elsif a && !b && c
my_object.some_method_because_of_a.some_method_because_of_c
etc...
So depending on a number of conditions I want to work out what methods to call in the method chain.
So far my best attempt to do this in a "good way" is to conditionally build the string of methods, and use eval
, but surely there is a better, more ruby, way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
您可以将方法放入数组中,然后执行该数组中的所有内容
Object#send
执行具有给定名称的方法。Enumerable#inject
迭代数组,同时为块提供最后返回的值和当前数组项。如果你希望你的方法接受参数,你也可以这样做
You could put your methods into an array and then execute everything in this array
Object#send
executes the method with the given name.Enumerable#inject
iterates over the array, while giving the block the last returned value and the current array item.If you want your method to take arguments you could also do it this way
您可以使用
tap
:You can use
tap
:使用
#yield_self
或者,从 Ruby 2.6 开始,使用#then
!Use
#yield_self
or, since Ruby 2.6,#then
!用于演示返回复制实例而不修改调用者的链接方法的示例类。
这可能是您的应用程序所需的库。
Monkeypatch:这是您要添加到应用程序中以启用条件链接的
内容 示例用法
Sample class to demonstrate chaining methods that return a copied instance without modifying the caller.
This might be a lib required by your app.
monkeypatch: this is what you want to add to your app to enable conditional chaining
Sample usage
尽管注入方法完全有效,但这种 Enumerable 的使用确实让人感到困惑,并且受到无法传递任意参数的限制。
像这样的模式可能更适合此应用程序:
我发现这在使用命名范围时很有用。例如:
Although the inject method is perfectly valid, that kind of Enumerable use does confuse people and suffers from the limitation of not being able to pass arbitrary parameters.
A pattern like this may be better for this application:
I've found this to be useful when using named scopes. For instance:
这是一种更函数式的编程方式。
使用
break
以使tap()
返回结果。 (点击仅在 Rails 中,如其他答案中提到的)Here's a more functional programming way.
Use
break
in order to gettap()
to return the result. (tap is in only in rails as is mentioned in the other answer)也许你的情况比这更复杂,但为什么不呢:
Maybe your situation is more complicated than this, but why not:
我使用这个模式:
I use this pattern:
如果您使用的是 Rails,则可以使用
#try
。而不是
write:
或带参数:
未在 vanilla ruby 中定义,但它 代码不多。
我不会为 CoffeeScript 的
?.
运算符提供什么。If you're using Rails, you can use
#try
. Instead ofwrite:
or, with arguments:
Not defined in vanilla ruby, but it isn't a lot of code.
What I wouldn't give for CoffeeScript's
?.
operator.我最终写了以下内容:
I ended up writing the following: