Created
November 15, 2010 17:03
-
-
Save aereal/700601 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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