diff options
Diffstat (limited to 'lib/core_ext')
-rw-r--r-- | lib/core_ext/json.rb | 13 |
1 files changed, 8 insertions, 5 deletions
diff --git a/lib/core_ext/json.rb b/lib/core_ext/json.rb index 3b08a04..1a82bd9 100644 --- a/lib/core_ext/json.rb +++ b/lib/core_ext/json.rb @@ -12,18 +12,21 @@ module JSON # def self.sorted_generate(obj) # modify hash and array - Hash.class_eval do + Array.class_eval do alias_method :each_without_sort, :each def each(&block) - keys.sort {|a,b| a.to_s <=> b.to_s }.each do |key| - yield key, self[key] + sorted = sort {|a,b| a.to_s <=> b.to_s } + for i in 0..(sorted.length-1) do + yield sorted[i] end end end - Array.class_eval do + Hash.class_eval do alias_method :each_without_sort, :each def each(&block) - sort {|a,b| a.to_s <=> b.to_s }.each_without_sort &block + self.keys.each do |key| + yield key, self.fetch(key) # fetch is used so we don't trigger Config::Object auto-eval + end end end |