Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop Pry from putting each value of a returned array on a new line?

Tags:

ruby

pry

I watched the RubyConf 2013 talk on Pry and I have decided I ought to give it a good try.

I am working with some large arrays. It would be easier to work with my code if Pry would display returned arrays the way IRB does. What seems odd is that pry will not add newlines if the number of chars in the displayed array is small but it will add them when the number of chars in the displayed array surpasses some threshold (appears to be 26 chars in my case). Does anybody know how to make Pry stop doing this?

IRB:

main 001(0) > a = [] #=> []
main 002(0) > (1..100).each{|i| a << i} #=> 1..100
main 003(0) > a
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]

Pry:

[1] pry(main)> a = []
=> []
[2] pry(main)> (1..26).each{ a << 1 }
=> 1..26
[3] pry(main)> a
=> [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
[4] pry(main)> a << 1
=> [1,
 1,
 1,
 1,
 1,
 1,
 1,
 1,
 1,
 1,
 1,
 1,
 1,
 1,
 1,
 1,
 1,
 1,
 1,
 1,
 1,
 1,
 1,
 1,
 1,
 1,
 1]
like image 374
Huliax Avatar asked Jan 02 '14 18:01

Huliax


2 Answers

Edit your .pryrc file to include

Pry.config.print = proc { |output, value| output.puts "=> #{value.inspect}" }
like image 83
robertjlooby Avatar answered Nov 15 '22 09:11

robertjlooby


To get pry to print inline like robertjlooby's answer, but retain the pretty-printing:

# in your .pryrc
Pry.config.print = lambda do |output, value, _pry_|
    _pry_.pager.open do |pager|
    pager.print _pry_.config.output_prefix
    Pry::ColorPrinter.pp(value, pager, 9e99)
  end
end

adapted from pry source

like image 45
Kache Avatar answered Nov 15 '22 08:11

Kache