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

ruby select mapの違い

0
Last updated at Posted at 2018-11-04
select

numbers = [1, 2, 3, 4, 5, 6, 7, 8]
p numbers.select {|item| item % 2 == 0 }

出力

[2, 4, 6, 8]
select_hash

バリュー軸

scores = { 'Alice' => 50, 'Bob' => 60, 'Carol' => 90, 'David' => 40 }
p scores.select {|k, v| v >= 60 }

出力

[["Bob", 60], ["Carol", 90]]   (Ruby 1.8の場合

キー軸

p scores.select {|k, v| k == 'Alice' }

出力
{"Alice"=>50}

map
numbers = ["68", "65", "6C", "6C", "6F"]
p numbers.map {|item| item.to_i(16) }

出力
[104, 101, 108, 108, 111]


selectとmapの掛け算

select&map

scores = { 'Alice' => 50, 'Bob' => 60, 'Carol' => 90, 'David' => 40 , 'joy' => 80}
p scores.select {|k, v| v >= 60 }.map{|m| m[1]}


出力

[60, 90, 80]

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