Top > Ruby [2.2.2] > include / member


Enumerable# include / member

p [1,2,3,4,5].include?(3)              # true
p ["1","2","3","4","5"].include?("3")  # true
p [1,2,3,4,5].include?(0)              # false
p ["1","2","3","4","5"].include?(3)    # false

p [1,2,3,4,5].member?(3)               # true
p ["1","2","3","4","5"].member?("3")   # true
p [1,2,3,4,5].member?(0)               # false
p ["1","2","3","4","5"].member?(3)     # false

test_hash = {"acha"=>100, "porute"=>200, "piipo"=>300}

p test_hash.include?('piipo')          #=> true
p test_hash.member?('orz')             #=> false
p test_hash.key?('acha')               #=> true
p test_hash.has_key?('porute')         #=> true

配列からデータを検索する時によく使用しています。便利なやつです。

"member" でもincludeと同じ事が可能です。
Hash内の検索にも使えます。Hashでは"key?"、"has_key?"も同じ事ができます。

検索データを抽出するなら、grep / scanを使います。



Copyright © 2015 AchaPorutePiipo All Rights Reserved.