Skip to content

Instantly share code, notes, and snippets.

@ravinggenius
Created January 22, 2015 22:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ravinggenius/70abb768676f7bdad1ee to your computer and use it in GitHub Desktop.
Save ravinggenius/70abb768676f7bdad1ee to your computer and use it in GitHub Desktop.
debug methods for rip
def self.debug(indent, label, context: nil, stack: nil)
body = if context && stack
[
"#{indent * 2} context",
*context_chain(indent * 4, context),
"#{indent * 2} stack",
*call_stack(indent * 4, stack)
]
elsif context
[
"#{indent * 2} context",
context_chain(indent * 4, context)
]
elsif stack
[
"#{indent * 2} stack",
call_stack(indent * 4, stack)
]
else
[]
end
[
(indent + label),
*body
]
end
def self.context_chain(indent, context)
extract_links(context).map do |link|
[
link.class,
link.object_id,
(link.origin if link.respond_to?(:origin)),
link.symbols.to_a
]
end.map do |(klass, object_id, origin, symbols)|
"#{indent}#{klass}:#{object_id} (#{origin}) [ #{symbols.sort.join(', ')} ]"
end
end
def self.extract_links(context)
if context.respond_to?(:outer_context) && context.outer_context
[
context,
*extract_links(context.outer_context)
]
else
[
context
]
end
end
def self.call_stack(indent, native_stack)
native_stack.select do |frame|
/rip-rip/ =~ frame
end.map do |frame|
indent + frame
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment