在Ruby语言的一个if语句中使用多个条件 [英] Using multiple conditions in one if-statement in Ruby Language

查看:257
本文介绍了在Ruby语言的一个if语句中使用多个条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用Ruby写这样的东西:

I write something like this in Ruby:

if a.max == a[0] 
  brand = b[0]
elsif a.max == a[1]
  brand = b[1]
elsif a.max == a[2]
  brand = b[2]
elsif a.max == a[3]
  brand = b[3]
end

ab都是唯一的数组.

有什么办法可以检查所有ifelsif是否处于相同状态?

Is there any way to check all if and elsif's in the same condition?

对于a[0]a[1]a[2]a[3],只有一个条件吗?

Only one condition for a[0], a[1], a[2] and a[3]?

推荐答案

Array#index might help in cases like these (assuming the size of a and b is the same):

brand = b[a.index(a.max)]

这篇关于在Ruby语言的一个if语句中使用多个条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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