34
21

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]配列をマージ・結合する

Posted at

配列を結合する(+, concat

>> a = ["ruby", "paython", "perl"]
=> ["ruby", "paython", "perl"]
>> b = ["paython", "perl", "php"]
=> ["paython", "perl", "php"]
>> a + b
=> ["ruby", "paython", "perl", "paython", "perl", "php"]
>> a.concat(b)
=> ["ruby", "paython", "perl", "paython", "perl", "php"]

配列をマージする(|, uniq

|を使う。

>> a = ["ruby", "paython", "perl"]
=> ["ruby", "paython", "perl"]
>> b = ["paython", "perl", "php"]
=> ["paython", "perl", "php"]
>> a | b
=> ["ruby", "paython", "perl", "php"]

uniqメソッドを使ってもできる。

>> a = ["ruby", "paython", "perl"]
=> ["ruby", "paython", "perl"]
>> b = ["paython", "perl", "php"]
=> ["paython", "perl", "php"]
>> (a + b).uniq
=> ["ruby", "paython", "perl", "php"]

|を使った書き方のほうが良さそうです。

34
21
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
34
21

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?