Created
January 14, 2010 05:12
-
-
Save jugyo/276899 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Hash | |
def to_sorted_hash | |
class << self | |
alias_method :__keys, :keys | |
def keys | |
self.__keys.sort_by {|k| k.to_s } | |
end | |
def values | |
self.keys.map {|k| self[k] } | |
end | |
end | |
self | |
end | |
end | |
sorted_hash = {:b => 'b', :a => 'a'}.to_sorted_hash | |
p sorted_hash.keys #=> [:a, :b] | |
p sorted_hash.values #=> ["a", "b"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment