Class: OrderedSet
- Inherits:
-
BlankSlate
- Object
- BlankSlate
- OrderedSet
- Defined in:
- lib/ramaze/snippets/ordered_set.rb
Overview
Basically an Set, but with Order, ain’t that obivous?
Class Method Summary (collapse)
Instance Method Summary (collapse)
- - (Object) []=(*args)
-
- (OrderedSet) initialize(*args)
constructor
Create new instances, optionally pass the first set.
-
- (Object) method_missing(meth, *args, &block)
Delegate everything, but controlled, keep elements unique.
Constructor Details
- (OrderedSet) initialize(*args)
Create new instances, optionally pass the first set
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/ramaze/snippets/ordered_set.rb', line 13 def initialize(*args) if args.size == 1 @set = args.shift else @set = *args end @set ||= [] @set = [@set] unless ::Array === @set @set.uniq! end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
- (Object) method_missing(meth, *args, &block)
Delegate everything, but controlled, keep elements unique. Warning, this is not really atomic.
48 49 50 |
# File 'lib/ramaze/snippets/ordered_set.rb', line 48 def method_missing(meth, *args, &block) @set.__send__(meth, *args, &block) end |
Class Method Details
+ (Object) [](*args)
8 9 10 |
# File 'lib/ramaze/snippets/ordered_set.rb', line 8 def self.[](*args) new(*args) end |
Instance Method Details
- (Object) []=(*args)
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/ramaze/snippets/ordered_set.rb', line 34 def []= *args @set.map! do |e| if ::Array === args.last args.last.include?(e) ? nil : e else args.last == e ? nil : e end end @set.__send__(:[]=, *args) @set.compact! end |