Sha256: ebe76f54bd9ffb75544f6be6adce1da724de2614a6f798215f39f841d39b68cc
Contents?: true
Size: 1.63 KB
Versions: 5
Compression:
Stored size: 1.63 KB
Contents
module AnnotatedArray module AnnotatedArrayItem attr_accessor :container, :container_index end def self.is_contained?(obj) AnnotatedArrayItem === obj end def annotate_item(obj, position = nil) return obj if obj.nil? obj = obj.dup if obj.frozen? obj.extend AnnotatedArray if Array === obj obj.extend AnnotatedArrayItem obj.container = self obj.container_index = position self.annotate(obj) end def [](pos, clean = false) item = super(pos) return item if item.nil? or clean annotate_item(item, pos) end def first annotate_item(super, 0) end def last annotate_item(super, self.length - 1) end def each_with_index(&block) super do |item,i| block.call annotate_item(item, i), i end end def select(&block) selected = [] each do |item| selected << item if block.call(item) end self.annotate(selected) end def each(&block) i = 0 super do |item| block.call annotate_item(item, i) i += 1 end end def inject(acc, &block) each do |item| acc = block.call acc, item end acc end def collect(&block) if block_given? inject([]){|acc,item| acc.push(block.call(item)); acc } else inject([]){|acc,item| acc.push(item); acc } end end def subset(list) self.annotate(self & list) end def remove(list) self.annotate(self - list) end %w(compact uniq flatten reverse sort_by).each do |method| self.define_method(method) do |*args,&block| res = super(*args, &block) annotate(res) res.extend AnnotatedArray res end end end
Version data entries
5 entries across 5 versions & 1 rubygems