module Kernel

Kernel extensions

Public Class Methods

pp_s(*objs) click to toggle source

Pretty print to a string.

Created by Graeme Mathieson.

See rha7dotcom.blogspot.com/2008/07/ruby-and-rails-how-to-get-pp-pretty.html

@example

pp_s(["foo","goo"])
=> "[\"foo\", \"goo\"]\n"

@return [String] a pretty print string of the params

# File lib/sixarm_ruby_ramp/kernel.rb, line 78
def pp_s(*objs)  
  str = StringIO.new  
  objs.each {|obj|  
      PP.pp(obj, str)  
    }  
    str.rewind  
    str.read  
end

Public Instance Methods

caller_method_name(caller_index=0) click to toggle source
See:
- http://www.ruby-forum.com/topic/75258
- In 1.9 (Ruby CVS HEAD) there is #__method__ and #__callee__
- http://eigenclass.org/hiki.rb?Changes+in+Ruby+1.9#l90

-

Make this fast because its often doing logging & reporting.
Inline this and use $1 because it's empirically faster than /1

These two methods must always be equal:
  caller_method_name(0) === my_method_name

@example
  def foo
    puts caller_method_name(0)
    puts caller_method_name(1)
  end
  => 
  "foo"
  "irb_binding"

@return [String] the method name of the caller at the index
# File lib/sixarm_ruby_ramp/kernel.rb, line 59
def caller_method_name(caller_index=0)
  RUBY_VERSION > '2.0' \
  ? caller_locations(caller_index + 1,1).first.base_label \
  : caller[caller_index][/`([^']*)'/, 1]
end
my_method_name() click to toggle source

See:

See stackoverflow.com/questions/5100299/how-to-get-the-name-of-the-calling-method

Make this fast because its often doing logging & reporting. Inline this and use $1 because it's empirically faster than /1

These two methods must always be equal:

caller_method_name(0) === my_method_name

@example

def foo
  puts my_method_name
end
foo
=> "foo"

@return [String] my method name

# File lib/sixarm_ruby_ramp/kernel.rb, line 30
def my_method_name
  RUBY_VERSION > '2.0' \
  ? caller_locations(1,1).first.base_label \
  : caller[0][/`([^']*)'/, 1]
end

Private Instance Methods

pp_s(*objs) click to toggle source

Pretty print to a string.

Created by Graeme Mathieson.

See rha7dotcom.blogspot.com/2008/07/ruby-and-rails-how-to-get-pp-pretty.html

@example

pp_s(["foo","goo"])
=> "[\"foo\", \"goo\"]\n"

@return [String] a pretty print string of the params

# File lib/sixarm_ruby_ramp/kernel.rb, line 78
def pp_s(*objs)  
  str = StringIO.new  
  objs.each {|obj|  
      PP.pp(obj, str)  
    }  
    str.rewind  
    str.read  
end