在的indexOf红宝石 [英] indexOf in Ruby

查看:228
本文介绍了在的indexOf红宝石的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想知道是否有一个阵列相同的方法对象在JavaScript中类似的indexOf?

例如:


ARR =%W {'A','B','C'}
C ='C'
如果(arr.indexOf(三)!= - 1)
//做一些东西
其他
//不要做一些东西


解决方案

这是的.index 阵列的方法。

HTTP://www.ruby- doc.org/core-1.9.3/Array.html#method-i-index

在红宝石,只有被认为是假值,所以你可能只是这样做:

  ARR =%W {A,B,C}
C ='C'
如果arr.indexÇ
  # 做一点事
其他
  #做别的事情
结束

Just wondering is there the same method for an Array object similar to indexOf in JavaScript?

For example:

arr = %w{'a', 'b', 'c'}
c = 'c'
if (arr.indexOf(c) != -1)
// do some stuff
else
// don't do some stuff

解决方案

It is the .index method of Array.

http://www.ruby-doc.org/core-1.9.3/Array.html#method-i-index

In ruby, only false and nil are considered as false value, so you could just do:

arr = %w{a, b, c}
c = 'c'
if arr.index c
  # do something
else 
  # do something else
end 

这篇关于在的indexOf红宝石的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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