/usr/lib/ruby/1.8/ramaze/snippets/ordered_set.rb is in libramaze-ruby1.8 2010.06.18-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | # Copyright (c) 2008 Michael Fellinger m.fellinger@gmail.com
# All files in this distribution are subject to the terms of the Ruby license.
require(File.join(File.dirname(__FILE__), 'blankslate'))
# Basically an Set, but with Order, ain't that obivous?
class OrderedSet < BlankSlate
def self.[](*args)
new(*args)
end
# Create new instances, optionally pass the first set
def initialize(*args)
if args.size == 1
@set = args.shift
else
@set = *args
end
@set ||= []
@set = [@set] unless ::Array === @set
@set.uniq!
end
%w[ push unshift << ].each do |meth|
class_eval %[
def #{meth} *args
@set.delete(*args)
@set.#{meth}(*args)
end
]
end
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
# Delegate everything, but controlled, keep elements unique.
# Warning, this is not really atomic.
def method_missing(meth, *args, &block)
@set.__send__(meth, *args, &block)
end
end
|