From 3c14c1b74764a328b893cadfbc7d8ce3d2f0c2e8 Mon Sep 17 00:00:00 2001 From: Mike Dvorkin Date: Tue, 9 Nov 2010 20:01:36 -0800 Subject: [PATCH] Replace array.inject with faster array.map --- lib/ap/awesome_print.rb | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/ap/awesome_print.rb b/lib/ap/awesome_print.rb index b7c575b..d437f5f 100755 --- a/lib/ap/awesome_print.rb +++ b/lib/ap/awesome_print.rb @@ -66,8 +66,7 @@ class AwesomePrint end "[\n" << data.join(",\n") << "\n#{outdent}]" else - data = a.inject([]) { |arr, item| arr << awesome(item) } - "[ #{data.join(', ')} ]" + "[ " << a.map{ |item| awesome(item) }.join(", ") << " ]" end end @@ -76,23 +75,23 @@ class AwesomePrint def awesome_hash(h) return "{}" if h == {} - data = h.keys.inject([]) do |arr, key| + data = h.keys.map do |key| plain_single_line do - arr << [ awesome(key), h[key] ] + [ awesome(key), h[key] ] end end width = data.map { |key, | key.size }.max || 0 width += @indentation if @options[:indent] > 0 - data = data.inject([]) do |arr, (key, value)| + data = data.map do |key, value| if @options[:multiline] formatted_key = (@options[:indent] >= 0 ? key.rjust(width) : indent + key.ljust(width)) else formatted_key = key end indented do - arr << (formatted_key << colorize(" => ", :hash) << awesome(value)) + formatted_key << colorize(" => ", :hash) << awesome(value) end end if @options[:multiline]