0
0

More than 1 year has passed since last update.

any?メソッド

Last updated at Posted at 2022-11-16

下のコメントは勉強になります(感謝!)

実際にないインスタンスを抽出する

irb(main):044:0> a = Model.where("column LIKE ?", "#{Date.today.year}-#{Date.today.month}%")
  Model Load (1.3ms)  SELECT `models`.* FROM `models` WHERE (column LIKE '2022-11%')
=> []   

クラスは?

irb(main):048:0> a.class
=> Model::ActiveRecord_Relation

中身は空かどうか?

irb(main):045:0> a.empty?
=> true

訂正 配列の中身の有無を判断にはempty?が最適(コメントより)

any? (instance method Array#any?)

any? -> bool
.
.
.
すべての要素が偽である場合に false を返します。真である要素があれば、ただちに true を返します。

ブロックを伴う場合は、各要素に対してブロックを評価し、すべての結果が偽である場合に false を返します。ブロックが真を返した時点で、ただちに true を返します。

p Set[].any?                             # => false

配列の中身を確認する

irb(main):046:0> a.any?
=> false

感想

また新しいメソッドを知ることができた。

出典

0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0