1
1

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 3 years have passed since last update.

ブロックを使う配列のメソッド

Posted at

####mapメソッド
戻り値をそのまま新しい変数に入れることができる。

ruby

numbers = [1,2,3,4,5]
new_numbers = numbers.map { |n| n * 10 }
puts new_numbers  #=> [10,20,30,40,50] 

####selectメソッド
各要素を評価し、真の要素を戻り値として配列にして返すメソッド
rejectメソッドはselectの反対で真になった要素を除外した配列を返す。

ruby
numbers = [1,2,3,4,5]
even_numbers = numbers.select { |n| n.even? }
puts even_numbers  #=>   [2,4]

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?