Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

自作ユーティリティー | 複数項目のor比較。SQLのinをJavaで再現 をRubyで書いてみた

サンプルコード

# encoding: Windows-31J

class String
  def in(*expects)
    raise "error" if expects[0].nil?
    return !(expects.index(self).nil?)
  end
end

def execute()
  puts "ほげ".in("ひげ")
  puts "ほげ".in("ほげ")
  puts "ほげ".in("はげ","ひげ","ふご")
  puts "ほげ".in("はげ","ほげ","ふご")
  puts "ほげ".in(nil)

  rescue
    puts "ERROR"
end

execute

出力

false
true
false
true
ERROR