Skip to content

Instantly share code, notes, and snippets.

@aereal
Created November 15, 2010 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aereal/700601 to your computer and use it in GitHub Desktop.
Save aereal/700601 to your computer and use it in GitHub Desktop.
class IndexHoldedList < ::Hash
def [](n)
raise ArgumentError unless Integer === n
self.fetch(n)
end
def []=(n, value)
raise ArgumentError unless Integer === n
self.store(n, value)
end
def each(&block)
self.values.each(&block)
end
def remove(n)
self[n] = nil
end
def prepend(val)
if self.any? {|i| i.nil? }
idx = self.find_index {|i| i.nil? }
self[idx] = val
else
self[(self.keys.sort.last || -1) + 1] = val
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment