2
0

More than 3 years have passed since last update.

[Ruby] all?メソッド

Last updated at Posted at 2021-08-22

はじめに

all?というメソッドを見つけました。
私的には少し使い方が変わっていると感じました。とても便利なので記事で残しておきます。

all?メソッド

配列に対して使用します。(配列でなくてもいいのかも?)
all?の後に{}を用意し、その中にブロック変数と条件式を記述します。
ブロック変数にall?直前の配列の要素を一つずつ放り込み、その配列の全ての要素が条件式でtrue 真だったときに初めてtrueを返します。

一つでもfalse 偽があるとfalseを返します。

array = [1, 2, 3, 4, 5, 6]
array.all?{|x| x > 0 }
=> true

array = [0, 1, 2, 3, 4, 5, 6]
array.all?{|x| x > 0 }
=> false

参考: https://docs.ruby-lang.org/ja/latest/method/Enumerable/i/all=3f.html

2
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
2
0