Ruby methods equivalent of “if a in list” in python?

前端 未结 2 1308
情深已故
情深已故 2020-12-30 02:06

In python I can use this to check if the element in list a:

>>> a = range(10)
>>> 5 in a
True
>>> 16 in a
False
         


        
2条回答
  •  粉色の甜心
    2020-12-30 03:09

    Use the include?() method:

    (1..10).include?(5) #=>true
    (1..10).include?(16) #=>false
    

    EDIT: (1..10) is Range in Ruby , in the case you want an Array(list) :

    (1..10).to_a #=> [1,2,3,4,5,6,7,8,9,10]
    

提交回复
热议问题