0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

様々な繰り返し処理

Posted at

目的

  • Enumberable処理について理解する

ポイント

  • Enumberable処理は、each などのブロックを呼び出してループを行う処理のこと
  • while文は 変数の初期化 や ループ終了のためのコード が必要
  • map はループの戻り値を配列で返す

書き方の例

numbers = [1, 2, 3]

count = 0
while count < numbers.length do
  puts numbers[count]
  count += 1
end

numbers.each do |number|
  puts number
end



~実際の表示~
1
2
3

1
2
3

注意するポイント

  • while文 はミスると無限ループになる
  • each文 はオブジェクトの数ぶんしかループしない

具体的な例

#配列の要素の数字1つ1つに10を足した結果を配列で出力
numbers = [1, 2, 3, 5, 6]

result =
numbers.map do |number|
  number + 10
end
p result


p result = numbers.map {|number| number + 10}


~実際の表示~
[11, 12, 13, 15, 16]
[11, 12, 13, 15, 16]

0
0
0

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?