Sha256: b89ffd304a3383281e033687e067c5b9c072f5f060cc6eac62bc51fef164b153

Contents?: true

Size: 1.27 KB

Versions: 142

Compression:

Stored size: 1.27 KB

Contents

require 'stringio'

#  Kernel
module Kernel
  # capture STDOUT
  #
  #
  # capture print
  #   print capture_stdout { print "hoge" } # => hoge
  #
  # if block have no STDOUT, capture_stdout returns empty.
  #   print capture_stdout {  }.empty? # => true
  def capture_stdout
    begin
      $stdout = StringIO.new
      yield
      result = $stdout.string
    ensure
      $stdout = STDOUT
    end
    result
  end

  # debug print line for print-debugging
  #
  #
  # debug print default
  #   dp_line __LINE__
  #   # yy = call line no
  #   # => --------------------|filename=|line=yy|--------------------
  #
  # debug print with filename
  #   dp_line __LINE__, filename: __FILE__
  #   # xx = filename, yy = call line no
  #   # => --------------------|filename=xx|line=yy|--------------------
  #
  # debug print with no filename, specific char
  #   dp_line __LINE__, char: '@'
  #   # xx = filename, yy = call line no
  #   # => @@@@@@@@@@@@@@@@@@@@|filename=|line=yy|@@@@@@@@@@@@@@@@@@@@
  def dp_line(line, options = { filename: '', char: '-' })
    filename = options[:filename].nil? ? '' : options[:filename]
    char = options[:char].nil? ? '-' : options[:char]
    puts "#{char * 20}|filename=#{filename}|line=#{line}|#{char * 20}"
  end
end

Version data entries

142 entries across 142 versions & 1 rubygems

Version Path
tbpgr_utils-0.0.151 lib/test_toolbox/kernel.rb
tbpgr_utils-0.0.150 lib/test_toolbox/kernel.rb
tbpgr_utils-0.0.149 lib/test_toolbox/kernel.rb
tbpgr_utils-0.0.148 lib/test_toolbox/kernel.rb
tbpgr_utils-0.0.147 lib/test_toolbox/kernel.rb
tbpgr_utils-0.0.146 lib/test_toolbox/kernel.rb
tbpgr_utils-0.0.145 lib/test_toolbox/kernel.rb
tbpgr_utils-0.0.144 lib/test_toolbox/kernel.rb
tbpgr_utils-0.0.143 lib/test_toolbox/kernel.rb
tbpgr_utils-0.0.142 lib/test_toolbox/kernel.rb
tbpgr_utils-0.0.141 lib/test_toolbox/kernel.rb
tbpgr_utils-0.0.140 lib/test_toolbox/kernel.rb
tbpgr_utils-0.0.139 lib/test_toolbox/kernel.rb
tbpgr_utils-0.0.138 lib/test_toolbox/kernel.rb
tbpgr_utils-0.0.137 lib/test_toolbox/kernel.rb
tbpgr_utils-0.0.136 lib/test_toolbox/kernel.rb
tbpgr_utils-0.0.135 lib/test_toolbox/kernel.rb
tbpgr_utils-0.0.134 lib/test_toolbox/kernel.rb
tbpgr_utils-0.0.133 lib/test_toolbox/kernel.rb
tbpgr_utils-0.0.132 lib/test_toolbox/kernel.rb