diff --git a/README.md b/README.md index 7e60824..a418e10 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,7 @@ multiline: true, # Display in multiple lines. plain: false, # Use colors. raw: false, # Do not recursively format instance variables. sort_keys: false, # Do not sort hash keys. +sort_vars: true, # Sort instance variables. limit: false, # Limit arrays & hashes. Accepts bool or int. ruby19_syntax: false, # Use Ruby 1.9 hash syntax in output. color: { diff --git a/lib/awesome_print/formatters/object_formatter.rb b/lib/awesome_print/formatters/object_formatter.rb index 655a8e6..a63d0f5 100644 --- a/lib/awesome_print/formatters/object_formatter.rb +++ b/lib/awesome_print/formatters/object_formatter.rb @@ -28,7 +28,7 @@ module AwesomePrint end end - data = vars.sort.map do |declaration, var| + data = (options[:sort_vars] ? vars.sort : vars).map do |declaration, var| key = left_aligned do align(declaration, declaration.size) end diff --git a/lib/awesome_print/inspector.rb b/lib/awesome_print/inspector.rb index 2a226b7..d0e131f 100644 --- a/lib/awesome_print/inspector.rb +++ b/lib/awesome_print/inspector.rb @@ -20,6 +20,7 @@ module AwesomePrint plain: false, # Use colors. raw: false, # Do not recursively format instance variables. sort_keys: false, # Do not sort hash keys. + sort_vars: true, # Sort instance variables. limit: false, # Limit arrays & hashes. Accepts bool or int. ruby19_syntax: false, # Use Ruby 1.9 hash syntax in output. color: { diff --git a/spec/objects_spec.rb b/spec/objects_spec.rb index 3aaef90..f4bdde4 100644 --- a/spec/objects_spec.rb +++ b/spec/objects_spec.rb @@ -127,6 +127,42 @@ EOS out = hello.ai(multiline: false, plain: true, raw: true) str = <<-EOS.strip # +EOS + expect(out).to be_similar_to(str) + expect(hello.ai(plain: true, raw: false)).to eq(hello.inspect) + end + + it 'without the sort_vars option does not sort instance variables' do + class Hello + attr_reader :abra + attr_writer :ca + attr_accessor :dabra + + def initialize + @abra = 1 + @ca = 2 + @dabra = 3 + @scooby = 3 + @dooby = 2 + @doo = 1 + end + + def instance_variables + [:@scooby, :@dooby, :@doo, :@abra, :@ca, :@dabra] + end + end + + hello = Hello.new + out = hello.ai(plain: true, raw: true, sort_vars: false) + str = <<-EOS.strip +# EOS expect(out).to be_similar_to(str) expect(hello.ai(plain: true, raw: false)).to eq(hello.inspect)