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.71 lib/test_toolbox/kernel.rb
tbpgr_utils-0.0.70 lib/test_toolbox/kernel.rb
tbpgr_utils-0.0.69 lib/test_toolbox/kernel.rb
tbpgr_utils-0.0.68 lib/test_toolbox/kernel.rb
tbpgr_utils-0.0.67 lib/test_toolbox/kernel.rb
tbpgr_utils-0.0.66 lib/test_toolbox/kernel.rb
tbpgr_utils-0.0.65 lib/test_toolbox/kernel.rb
tbpgr_utils-0.0.64 lib/test_toolbox/kernel.rb
tbpgr_utils-0.0.63 lib/test_toolbox/kernel.rb
tbpgr_utils-0.0.62 lib/test_toolbox/kernel.rb
tbpgr_utils-0.0.61 lib/test_toolbox/kernel.rb
tbpgr_utils-0.0.60 lib/test_toolbox/kernel.rb
tbpgr_utils-0.0.59 lib/test_toolbox/kernel.rb
tbpgr_utils-0.0.58 lib/test_toolbox/kernel.rb
tbpgr_utils-0.0.57 lib/test_toolbox/kernel.rb
tbpgr_utils-0.0.56 lib/test_toolbox/kernel.rb
tbpgr_utils-0.0.55 lib/test_toolbox/kernel.rb
tbpgr_utils-0.0.54 lib/test_toolbox/kernel.rb
tbpgr_utils-0.0.53 lib/test_toolbox/kernel.rb
tbpgr_utils-0.0.52 lib/test_toolbox/kernel.rb