Search code examples
ruby-on-railspry

How to hide pry scope?


Sometimes when I open a binding.pry session in a spec it shows me a too long scope in the shell like the following and takes all the shell line:

[3] pry(#<RSpec::ExampleGroups::Scope::AfterSeed::BehavesLikeAnExportableToExcelModel::WhenImportingFromSpreadsheet::AXlsxFile>)>

It turns impossible to use the shell when the level is too deep, and this scope information becomes pointless. How can I hide this scope to something like pry(#hidden_info>)> and still have the methods and variables from the previous scope?


Solution

  • The pry prompt can be configured in a variety of ways. The prompt value can take any arbitrary form. (the limit is whatever you can do in Ruby)

    You can see the DEFAULT_PROMPT values in the rdocs:

    [
     proc { |target_self, nest_level, pry|
       "[#{pry.input_array.size}] #{pry.config.prompt_name}(#{Pry.view_clip(target_self)})#{":#{nest_level}" unless nest_level.zero?}> "
     },
    
     proc { |target_self, nest_level, pry|
       "[#{pry.input_array.size}] #{pry.config.prompt_name}(#{Pry.view_clip(target_self)})#{":#{nest_level}" unless nest_level.zero?}* "
     }
    ]
    

    In your case, you would probably want to put the following into ~/.pryrc and then restart any pry sessions:

    Pry.config.prompt = [
     proc { |target_self, nest_level, pry|
       "[#{pry.input_array.size}] #{pry.config.prompt_name}#{":#{nest_level}" unless nest_level.zero?}> "
     },
    
     proc { |target_self, nest_level, pry|
       "[#{pry.input_array.size}] #{pry.config.prompt_name}#{":#{nest_level}" unless nest_level.zero?}* "
     }
    ]
    

    This removes the Pry.view_clip(target_self) call which will clip the class from the prompt.

    There are examples here on Stack Overflow for configuring the pry prompt as well.