Created
May 13, 2010 00:14
-
-
Save reinh/399304 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 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