🖥

#Ruby で配列の和集合・積集合・差集合を、記号じゃなくて、名前のあるメソッドで得る

2019/07/06に公開

記号ってわかりにくいじゃないですか

require

require 'set'

和集合

Set.new([1,2,3]).union Set.new([2,3,4])
# => #<Set: {1, 2, 3, 4}>

(Set.new([1,2,3]).union Set.new([2,3,4])).to_a
# => [1, 2, 3, 4]

積集合


Set.new([1,2,3]).intersection Set.new([2,3,4])
=> #<Set: {2, 3}>

Set.new([1,2,3]).intersection Set.new([2,3,4])).to_a
=> [2, 3]

差集合

Set.new([1,2,3]).difference Set.new([2,3,4])
# => #<Set: {1}>

(Set.new([1,2,3]).difference Set.new([2,3,4])).to_a
# => [1]

Ref

class Set (Ruby 2.6.0)

Original by Github issue

https://github.com/YumaInaura/YumaInaura/issues/2251

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

https://line.me/ti/g2/eEPltQ6Tzh3pYAZV8JXKZqc7PJ6L0rpm573dcQ

Twitter

https://twitter.com/YumaInaura

公開日時

2019-07-06

Discussion