From: Stefano Zacchiroli Date: 2007-01-15T22:29:11+09:00 Subject: Re: newbie question about sets On Mon, Jan 15, 2007 at 10:07:47PM +0900, Xavier Noria wrote: > Yes, that implements sets as defined in math. What you wanted is > called "multiset"[*], though in my degree I never saw any of those > even mentioned. I guess one can build them by herself on the following lines: class MultiSet < Set def initialize super @arity = {} end attr_reader :arity def add(o) super(o) @arity[o] = 0 unless @arity.member? o @arity[o] += 1 end def delete(o) super(o) @arity[o] -= 1 @arity.delete o if @arity[o] == 0 end end but beware that the above is newbie's code (and also utterly simplified ...) :-) And by the way, why the above class does not work properly with << (which is an alias for add according to my set.rb) while it works properly with add? E.g. irb(main):019:0> m=MultiSet.new => # irb(main):020:0> m.add 1 => 1 irb(main):021:0> m.add 1 => 2 irb(main):022:0> m.arity[1] => 2 # so far so good irb(main):023:0> m << 2 => # irb(main):024:0> m << 2 => # irb(main):025:0> m.arity[2] => nil # aargh, do I need to alias again in # derived classes? that's too bad ... TIA, Cheers. -- Stefano Zacchiroli -*- Computer Science PhD student @ Uny Bologna, Italy zack@{cs.unibo.it,debian.org,bononia.it} -%- http://www.bononia.it/zack/ (15:56:48) Zack: e la demo dema ? /\ All one has to do is hit the (15:57:15) Bac: no, la demo scema \/ right keys at the right time