Module: Wacktrace
- Defined in:
- lib/wacktrace.rb,
lib/wacktrace/version.rb
Defined Under Namespace
Classes: Error
Constant Summary collapse
- VERSION =
"1.0.1"
Class Method Summary collapse
-
.add_to_stack(lines, &real) ⇒ Object
Runs the given block with the given lines inserted into the call stack above it.
-
.add_to_stack_from_lyrics(lyrics, filename, &block) ⇒ Object
This is a convenience method to construct a stack trace from a single string with a bunch of newlines (eg the lyrics to a song or words to a poem).
Class Method Details
.add_to_stack(lines, &real) ⇒ Object
Runs the given block with the given lines inserted into the call stack above it. Lines should be an array like:
[
["somemethod1", 111, "somefile1"],
["somemethod2", 222, "somefile2"],
["somemethod3", 333, "somefile3"],
]
That will result in a strack trace like:
"somefile3:333:in ` somemethod3'",
"somefile2:222:in ` somemethod2'",
"somefile1:111:in ` somemethod1'",
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/wacktrace.rb', line 23 def add_to_stack(lines, &real) if lines.length <= 0 return real.call end # If stack traces are being printed newest-to-oldest, we need to reverse # the input lines in order to have them print out readably. if detect_order == :recent_first lines.reverse! end lines = fix_duplicate_methods(lines) # Create a namespace for all these methods so we don't polute the global # namespace. container_class = Class.new raise "add to stack missing block" unless block_given? lines = lines.map { |line| [clean_method_name(line[0]), line[1], line[2]] } # Define each method in series: a calls b, b calls c, and so on. The last # line does not get a method defined here. lines.each_cons(2) do |line_1, line_2| # puts "defining '#{line_1[0]}'" success = true define_stack_level(container_class, line_1, line_2[0]) end last = lines.last define_stack_level(container_class, last, 'ending') container_class.define_singleton_method("ending") { real.call } return container_class.send(lines.first[0]) end |
.add_to_stack_from_lyrics(lyrics, filename, &block) ⇒ Object
This is a convenience method to construct a stack trace from a single string with a bunch of newlines (eg the lyrics to a song or words to a poem). It’ll use the same filename for each line.
61 62 63 64 65 66 |
# File 'lib/wacktrace.rb', line 61 def add_to_stack_from_lyrics(lyrics, filename, &block) lines = lyrics.split("\n").map.with_index do |line, i| [line, i, filename] end add_to_stack(lines, &block) end |