Ruby:调用方法列表,直到返回 true [英] Ruby: call list of methods until one returns true

查看:53
本文介绍了Ruby:调用方法列表,直到返回 true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种很好的方法可以在 ruby​​ 中进行一系列方法调用,直到一个返回 true?

Is there a pretty way to make a series of method calls in ruby UNTIL one returns true?

这是我的第一个想法,但我认为可能有更好的方法:

This was my first thought, but was thinking there might be a nicer way:

if method_one
elsif method_two
elsif method_three
else
  puts "none worked"
end

推荐答案

你可以使用 Enumerable#any?

You can use Enumerable#any? as well.

[ :m1, :m2, :m3 ].any?{ |method| object.send( method )} || "None Worked"

这篇关于Ruby:调用方法列表,直到返回 true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆