awesome-print / awesome_print

Pretty print your Ruby objects with style -- in full color and with proper indentation
http://github.com/michaeldv/awesome_print
MIT License
4.08k stars 454 forks source link

Defaults ignored if set in ~/.irbrc #98

Closed nickhoffman closed 12 years ago

nickhoffman commented 12 years ago

Hey there. If I set AP defaults within ~/.irbrc and then boot up an IRB session, the defaults have been set, but they aren't used:

$ grep AwesomePrint -A 3 ~/.irbrc 
if defined? AwesomePrint
  AwesomePrint.defaults = {
    :indent => -2,
  }
end
irb> AwesomePrint.defaults
 => {:indent=>-2}
irb>
irb> h = {:a => 'eh', :bee => {:one => 1}}
irb>
irb> ap h  # The output here is wrong. It should be left-aligned, and use only 2 spaces for indentation.
{
      :a => "eh",
    :bee => {
        :one => 1
    }
}
irb>
irb> ap h, :indent => -2  # This is what's specified in the defaults.
{
  :a   => "eh",
  :bee => {
    :one => 1
  }
}
CoderPuppy commented 12 years ago

It's because of how ap merges in the defaults. A quick fix would be to add merge_options! (AwesomePrint.defaults) to AwesomePrint::Inspector#initialize

nickhoffman commented 12 years ago

@drewyoung1, is that an ideal solution, or would it be considered a bit of a kludge by the maintainers?

CoderPuppy commented 12 years ago

@nickhoffman, I think its good, but maybe there should be an if AwesomePrint.defaults.is_a? Hash at the end. But there may be other better solutions, this is just from maybe a minute or two of looking at the code that merges it.

michaeldv commented 12 years ago

I've posted the fix: https://github.com/michaeldv/awesome_print/commit/a939430d166f900a54d5ff6032d92767deb19900

The defaults were expected to come from ~/.aprc but now I merge them if they're set somewhere else.

Thanks, Michael

nickhoffman commented 12 years ago

Thanks, @michaeldv!