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?

【Ruby】最近発見した便利な記述方法メモ

Posted at

ここ数日qiitaを巡回していて見つけたRubyで完結に書ける実装方法メモ。

|

array1 = ["a", "b"]
array2 = ["b", "c", "d"]

array1 | array2
# => ["a", "b", "c", "d"]

配列と配列を結合して1つの配列を返す。
重複する要素は1つにまとめられる。

&

array1 = ["a", "b"]
array2 = ["b", "c", "d"]

array1 & array2
# => ["b"]

共通する要素のみを取り出して配列として返す。

array.map(&:method)

array = ["a","b", "c"]
array.map(&:upcase)
# => ["A", "B", "C"]

array.map(&: * 2)
# => error
# mrthodじゃないから処理できない

array.map{|item| item.method}を簡潔に書くことができる。

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?