Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
370 views
in Technique[技术] by (71.8m points)

ruby - 检查Ruby中的数组中是否存在值(Check if a value exists in an array in Ruby)

I have a value 'Dog' and an array ['Cat', 'Dog', 'Bird'] .

(我有一个值'Dog'和一个阵列['Cat', 'Dog', 'Bird'] 。)

How do I check if it exists in the array without looping through it?

(如何在没有循环的情况下检查数组中是否存在?)

Is there a simple way of checking if the value exists, nothing more?

(是否有一种简单的方法来检查值是否存在,仅此而已?)

  ask by user211662 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You're looking for include?

(你在寻找include?)

:

(:)

>> ['Cat', 'Dog', 'Bird'].include? 'Dog'
=> true

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...