From c9781fc974dbdb82bbf6edbec073b9723afa7377 Mon Sep 17 00:00:00 2001 From: Mike Dvorkin Date: Wed, 12 May 2010 22:03:33 -0700 Subject: [PATCH] Avoid Struct redifinition warning when running specs; removed unnecessary code --- lib/ap/awesome_print.rb | 14 +++++--------- spec/awesome_print_spec.rb | 7 +++++-- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/lib/ap/awesome_print.rb b/lib/ap/awesome_print.rb index 0ea5bf7..07db7f7 100755 --- a/lib/ap/awesome_print.rb +++ b/lib/ap/awesome_print.rb @@ -161,8 +161,8 @@ class AwesomePrint #------------------------------------------------------------------------------ def nested(object) case printable(object) - when :array then colorize("[...]", :array) - when :hash then colorize("{...}", :hash) + when :array then colorize("[...]", :array) + when :hash then colorize("{...}", :hash) when :struct then colorize("{...}", :struct) else colorize("...#{object.class}...", :class) end @@ -177,20 +177,16 @@ class AwesomePrint # Turn class name into symbol, ex: Hello::World => :hello_world. #------------------------------------------------------------------------------ def declassify(object) - if object.class.to_s.downcase =~ /^struct/ - result = :struct + if object.is_a?(Struct) + :struct else - result = object.class.to_s.gsub(/:+/, "_").downcase.to_sym + object.class.to_s.gsub(/:+/, "_").downcase.to_sym end - result end # Pick the color and apply it to the given string as necessary. #------------------------------------------------------------------------------ def colorize(s, type) - if type && !type.to_s.empty? - type = type.to_s.gsub(/^struct_.*/,'struct').to_sym - end if @options[:plain] || @options[:color][type].nil? s else diff --git a/spec/awesome_print_spec.rb b/spec/awesome_print_spec.rb index b63d60c..d49fbcc 100644 --- a/spec/awesome_print_spec.rb +++ b/spec/awesome_print_spec.rb @@ -292,8 +292,11 @@ EOS #------------------------------------------------------------------------------ describe "Struct" do before(:each) do - struct = Struct.new("SimpleStruct", :name, :address) - @struct = struct.new + @struct = unless defined?(Struct::SimpleStruct) + Struct.new("SimpleStruct", :name, :address).new + else + Struct::SimpleStruct.new + end @struct.name = "Herman Munster" @struct.address = "1313 Mockingbird Lane" end