Tbpgr Blog

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

ActiveSupport | Object#in?

概要

Object#in?

詳細

Object#in?について

Object#in?

対象オブジェクトのinclude?を呼び出し、結果を真偽値で返却します。
配列、文字列、範囲など種類を問わず指定した値が含まれているか判定したい場合に有用です。

サンプル

# encoding: utf-8
require "active_support/core_ext/object/inclusion"
require 'tbpgr_utils'

puts_eval '"eab".in? "hogeable"', binding
puts_eval '"hige".in? "hogeable"', binding
puts_eval '"hoge".in? ["hoge", "hige"]', binding
puts_eval '"hage".in? ["hoge", "hige"]', binding
puts_eval '5.in? (1..5)', binding
puts_eval '6.in? (1..5)', binding

__END__
下記はTbpgrUtils gemの機能
puts_eval

https://rubygems.org/gems/tbpgr_utils
https://github.com/tbpgr/tbpgr_utils

出力

"eab".in? "hogeable" # => true
"hige".in? "hogeable" # => false
"hoge".in? ["hoge", "hige"] # => true
"hage".in? ["hoge", "hige"] # => false
5.in? (1..5) # => true
6.in? (1..5) # => false