Find value in an array

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

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

10条回答
  •  不知归路
    2020-12-24 00:46

    This answer is for everyone that realizes the accepted answer does not address the question as it currently written.

    The question asks how to find a value in an array. The accepted answer shows how to check whether a value exists in an array.

    There is already an example using index, so I am providing an example using the select method.

    1.9.3-p327 :012 > x = [1,2,3,4,5]
      => [1, 2, 3, 4, 5] 
    1.9.3-p327 :013 > x.select {|y| y == 1}
      => [1]
    

提交回复
热议问题