irb(main):???:0> Multiset[:a, :a, :b, :c, :b] => #<Multiset:#2 :b, #1 :c, #2 :a>
Enumerableであるオブジェクトであれば、配列以外でも指定できます。
irb(main):???:0> arr = [:a, :a, :b, :c, :b] => [:a, :a, :b, :c, :b] irb(main):???:0> Multiset.new(arr) => #<Multiset:#2 :b, #1 :c, #2 :a>
irb(main):???:0> hash = { :a => 2, :b => 2, :c => 1 }
=> {:b=>2, :c=>1, :a=>2}
irb(main):???:0> hash.to_multiset
=> #<Multiset:#2 :b, #1 :c, #2 :a>
irb(main):???:0> ms = Multiset[:a, :a, :b, :c, :b] => #<Multiset:#2 :a, #2 :b, #1 :c> irb(main):???:0> ms2 = ms.dup => #<Multiset:#2 :a, #2 :b, #1 :c> irb(main):???:0> ms == ms2 => true irb(main):???:0> ms.object_id == ms2.object_id => false
irb(main):???:0> ms = Multiset[:a, :a, :b, :c, :b] => #<Multiset:#2 :a, #2 :b, #1 :c> irb(main):???:0> ms.replace Multiset[:x, :x, :y] => #<Multiset:#2 :x, #1 :y>
irb(main):???:0> ms = Multiset[:a, :a, :b, :c, :b] => #<Multiset:#2 :b, #1 :c, #2 :a> irb(main):???:0> ms.add(:a) => #<Multiset:#2 :b, #1 :c, #3 :a> irb(main):???:0> ms << :b => #<Multiset:#3 :b, #1 :c, #3 :a>
同一の要素を複数追加するには、addメソッドの第2引数を指定します。
irb(main):???:0> ms = Multiset[:a, :a, :b, :c, :b] => #<Multiset:#2 :b, #1 :c, #2 :a> irb(main):???:0> ms.add(:c, 6) => #<Multiset:#2 :b, #7 :c, #2 :a>
addメソッドと同様、deleteメソッドでも第2引数が指定できます。またdelete_allメソッドで該当する要素をすべて削除します。
irb(main):???:0> ms = Multiset[:a, :a, :b, :c, :b] => #<Multiset:#2 :b, #1 :c, #2 :a> irb(main):???:0> ms.delete(:a) => #<Multiset:#2 :b, #1 :c, #1 :a> irb(main):???:0> ms.delete(:b, 2) => #<Multiset:#1 :c, #1 :a> irb(main):???:0> ms.delete_all(:c) => #<Multiset:#1 :a>
irb(main):???:0> ms = Multiset[:a, :a, :b, :c, :b]
=> #<Multiset:#2 :b, #1 :c, #2 :a>
irb(main):???:0> ms.delete_if{ |item| item.to_s < "c" }
=> #<Multiset:#1 :c>
irb(main):???:0> ms = Multiset[:a, :a, :b, :c, :b]
=> #<Multiset:#2 :b, #1 :c, #2 :a>
irb(main):???:0> ms.delete_with{ |item, count| count < 2 }
=> #<Multiset:#2 :b, #2 :a>
irb(main):???:0> ms = Multiset[:a, :a, :b, :c, :b] => #<Multiset:#2 :b, #1 :c, #2 :a> irb(main):???:0> ms.renew_count(:c, 10) => #<Multiset:#2 :b, #10 :c, #2 :a>
irb(main):???:0> Multiset[:a, :a, :b, :c, :b] + Multiset[:a, :b, :x, :y] => #<Multiset:#3 :a, #3 :b, #1 :x, #1 :c, #1 :y>
irb(main):???:0> Multiset[:a, :a, :b, :c, :b].size => 5 irb(main):???:0> Multiset[:a, :a, :b, :c, :b].count(:a) => 2
多重集合が空であるか調べる際は、empty?メソッドを使う。
irb(main):???:0> Multiset[:a, :a, :b, :c, :b].empty? => false irb(main):???:0> Multiset[].empty? => true
irb(main):???:0> Multiset[:a, :a, :b, :c, :b].include?(:a) => true irb(main):???:0> Multiset[:a, :a, :b, :c, :b].include?(:x) => false
irb(main):???:0> Multiset[:a, :a, :b, :c, :b].rand => :b irb(main):???:0> Multiset[:a, :a, :b, :c, :b].rand => :a
irb(main):???:0> Multiset[:a, :a, :b, :c, :b].to_a => [:b, :b, :c, :a, :a]
重複する要素を除去する場合
irb(main):???:0> Multiset[:a, :a, :b, :c, :b].items => [:b, :c, :a]
キーが要素、値が個数となります。
irb(main):???:0> Multiset[:a, :a, :b, :c, :b].to_hash
=> {:b=>2, :c=>1, :a=>2}
irb(main):???:0> Multiset[:a, :a, :b, :c, :b].to_set
=> #<Set: {:a, :b, :c}>
irb(main):???:0> ms = Multiset[:a, :a, :b, :c, :b]
=> #<Multiset:#2 :b, #1 :c, #2 :a>
irb(main):???:0> ms.each { |i| p i }
:b
:b
:c
:a
:a
=> #<Multiset:#2 :b, #1 :c, #2 :a>
irb(main):???:0> ms = Multiset[:a, :a, :b, :c, :b]
=> #<Multiset:#2 :b, #1 :c, #2 :a>
irb(main):???:0> ms.each_item { |i| p i }
:b
:c
:a
=> #<Multiset:#2 :b, #1 :c, #2 :a>
irb(main):???:0> ms = Multiset[:a, :a, :b, :c, :b]
=> #<Multiset:#2 :b, #1 :c, #2 :a>
irb(main):???:0> ms.each_pair { |i, c| p [i, c] }
[:b, 2]
[:c, 1]
[:a, 2]
=> #<Multiset:#2 :b, #1 :c, #2 :a>
irb(main):???:0> ms = Multiset[:a, :a, :b, :c, :b]
=> #<Multiset:#2 :a, #2 :b, #1 :c>
irb(main):???:0> ms.map{ |i| item.to_s }
=> #<Multiset:#2 "a", #2 "b", #1 "c">
irb(main):???:0> ms = Multiset[:a, :a, :b, :c, :b]
=> #<Multiset:#2 :a, #2 :b, #1 :c>
irb(main):???:0> ms.map_with{ |i, c| [item.to_s, count * 2] }
=> #<Multiset:#4 "a", #4 "b", #2 "c">
irb(main):???:0> Multiset[:a, :a, :b, :c, :b] == Multiset[:a, :b, :c, :b, :a] => true irb(main):???:0> Multiset[:a, :a, :b, :c, :b] == Multiset[:a, :b, :c, :b] => false
irb(main):???:0> Multiset[:a, :a, :b, :c].subset?(Multiset[:a, :b, :c, :b, :a]) => true irb(main):???:0> Multiset[:a, :a, :b, :c, :b].superset?(Multiset[:a, :b, :c, :b]) => true
この他にも、proper_subset?やproper_superset?といったメソッドがあります。
irb(main):???:0> Multiset[:a, :a, :b, :c, :b].classify{ |i| i.to_s < "c" }
=> {false=>#<Multiset:#1 :c>, true=>#<Multiset:#2 :a, #2 :b>}
irb(main):???:0> Multiset[:a, :a, :b, :c, :b].classify_with{ |i, c| c }
=> {1=>#<Multiset:#1 :c>, 2=>#<Multiset:#2 :a, #2 :b>}