Find value in an array

后端 未结 10 1855
生来不讨喜
生来不讨喜 2020-12-23 23:52

In Ruby, how can I find a value in an array?

10条回答
  •  猫巷女王i
    2020-12-24 00:36

    I'm guessing that you're trying to find if a certain value exists inside the array, and if that's the case, you can use Array#include?(value):

    a = [1,2,3,4,5]
    a.include?(3)   # => true
    a.include?(9)   # => false
    

    If you mean something else, check the Ruby Array API

提交回复
热议问题