Truncate #inspect output in irb (ruby)

I want to trim the output of #inspect to irb (large output should be trimmed to MAX_LEN).

I am currently redefining: checking methods: to_s for all specific objects.

Is there any other solution?

  • change $ stdout?
  • other?
+3
source share
6 answers

For a clean solution gem install hirb. hirb irb pages return values ​​if they are too long.

If you want monkeypatch irb:

module IRB
  class Irb
    def output_value
     @context.last_value.to_s.slice(0, MAX_LEN)
    end
  end
end

I do not recommend this because it is hackable and breakable anytime gems like ap and hirb are required.

monkeypatching irb, ripl, irb, . ripl:

require 'ripl'
module Ripl::SlicedInspect
  def format_result(result)
    result_prompt + result.inspect.slice(MAX_LEN)
  end
end
Ripl::Shell.send :include, Ripl::SlicedInspect

~/.riplrc, .

+5

.

, .

+1

IRB - monkeypatch irb , "load". , , - , ....

+1

, -, , awesome_print. irb, .irbrc :

require 'ap'

module IRB
  class Irb
    def output_value
      ap @context.last_value
    end
  end
end

IRB.

awesome_print, , , to_s .

+1

3.1.1+ helpers/irb_helper.rb

module IRB
  class Irb
    MAX_LEN = 10000

    def output_value
      if (@context.inspect_last_value.length > MAX_LEN)
        printf @context.return_format, "#{@context.inspect_last_value[0..MAX_LEN]} <- Truncated"
      else
        printf @context.return_format, @context.inspect_last_value
      end
    end
  end
end

, irb https://github.com/Ruby/Ruby/blob/trunk/lib/irb.rb

+1

I sometimes modify the objects themselves (through a module with the name BoringInspectthat I am includein the appropriate classes), so the exception messages are also manageable.

0
source

Source: https://habr.com/ru/post/1779135/


All Articles