Sha256: 2f3770467ad67aa7dd8e19cc48cd3ac317c8969166ef8f9f7226af606b7439a6

Contents?: true

Size: 1.77 KB

Versions: 5

Compression:

Stored size: 1.77 KB

Contents

# TITLE:
#
#   Reporting Extensions
#
# DESCRIPTION:
#
#   Kernel extensions for debugging, warning and demoing.
#
# AUTHORS:
#
#   - TransDelabug

module Kernel
  module_function

  # Redefines standrard #p kernel method
  # to pass through its argument.
  #
  # DEPRECATE AS OF 1.9
  def p(x)
    puts x.inspect
    x
  end

  # Like #p but gives file and line number.

  def d(x)
    puts "#{__FILE__}, #{__LINE__}: #{x.inspect}"
  end

  # Pretty prints an exception/error object,
  # useful for helpfull debug messages.
  #
  # Input:
  # The Exception/StandardError object
  #
  # Output:
  # The pretty printed string.

  def pp_exception(ex)
    return %{#{ex.message}\n  #{ex.backtrace.join("\n  ")}\n  LOGGED FROM: #{caller[0]}}
  end

  # For debugging and showing examples. Currently this
  # takes an argument of a string in a block.
  #
  #   demo {%{ a = [1,2,3] }}
  #   demo {%{ a.slice(1,2) }}
  #   demo {%{ a.map { |x| x**3 } }}
  #
  # Produces:
  #
  #   a = [1,2,3]             #=>  [1, 2, 3]
  #   a.slice(1,2)            #=>  [2, 3]
  #   a.map { |x| x**3 }      #=>  [1, 8, 27]
  #
  #--
  # Is there a way to do this without the eval string in block?
  # Preferably just a block and no string.
  #++

  def demo(out=$stdout,&block)
    out << sprintf("%-25s#=>  %s\n", expr = block.call, eval(expr, block.binding).inspect)
  end

  # Like #warn produces the current line number as well.
  #
  #   warn_with_line("You have been warned.")
  #
  # _produces_
  #
  #   3: Warning: You have been warned.
  #
  # Note that this method depends on the output of #caller.

  def warn_with_line(msg="", fulltrace=nil)
    trace = caller(1)
    where = trace[0].sub(/:in.*/,'')
    STDERR.puts "#{where}: Warning: #{msg}"
    STDERR.puts trace.map { |t| "\tfrom #{t}" } if fulltrace
  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
facets-2.0.0 lib/core/facets/kernel/report.rb
facets-2.0.1 lib/core/facets/kernel/report.rb
facets-2.0.2 lib/core/facets/kernel/report.rb
facets-2.0.3 lib/core/facets/kernel/report.rb
facets-2.0.4 lib/core/facets/kernel/report.rb