Skip to content

Instantly share code, notes, and snippets.

@reinh
Created May 13, 2010 00:14
Show Gist options
  • Save reinh/399304 to your computer and use it in GitHub Desktop.
Save reinh/399304 to your computer and use it in GitHub Desktop.
class SortedArray < Array
def <<(other)
super
sort!
end
def push(other)
super
sort!
end
end
# BUT! SortedArray would then have all of the methods of Array, many of which
# don't make sense for a sorted array, like []=. Better would be to have it delegate:
class SortedArray < BasicObject
include Forwardable
def initialize(array)
@array = array
end
def <<(other)
@array << other
@array.sort!
end
def_delegators :@array, :[], :length, :size # delegate useful methods to array here
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment