Skip to content

Instantly share code, notes, and snippets.

@haileys
Created March 3, 2014 09:09
Show Gist options
  • Save haileys/9321188 to your computer and use it in GitHub Desktop.
Save haileys/9321188 to your computer and use it in GitHub Desktop.
require "binding_of_caller"
def baz(e, f)
1 + "lol"
end
def bar(c:, d:)
baz(c, d)
end
def foo(a: 1, b: 2)
bar(c: a, d: b)
end
module KeywordBacktrace
prepend_features Exception
def set_backtrace(bt)
if ArgumentError === self && message == "no such frame"
return super
end
super(binding.callers.drop(1).map { |b|
next unless iseq = b.instance_variable_get(:@iseq)
label = iseq.label
if mid = b.eval("__method__")
method = b.eval("self").method(mid)
keyword_arguments = method.parameters.select { |type, name|
type == :key || type == :keyreq
}.map { |type, name|
"#{name}:"
}
if keyword_arguments.any?
label += "(#{keyword_arguments.join(", ")})"
end
end
"#{iseq.path}:#{iseq.first_lineno}: in `#{label}'"
}.compact)
end
end
foo(a: 1, b: 2)
# lol.rb:3: in `baz': String can't be coerced into Fixnum (TypeError)
# from lol.rb:7: in `bar(c:, d:)'
# from lol.rb:11: in `foo(a:, b:)'
# from lol.rb:0: in `<main>'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment