pry/pry · error · CommandError
Could not find original `def #{method.original_name}` line t
Error message
Could not find original `def #{method.original_name}` line to patch. What it means
`edit-method` re-defines a method by rewriting its source in the owner's context. To do that, Pry::Method::Patcher must match the original definition line against `def [<receiver>.]<original_name>` and rewrite it. When the first source line doesn't literally match that pattern — define_method bodies, aliases where original_name differs from the visible def, shifted source_location — CommandError is raised and no patch is applied.
Source
Thrown at lib/pry/method/patcher.rb:82
end
# Update the definition line so that it can be eval'd directly on the Method's
# owner instead of from the original context.
#
# In particular this takes `def self.foo` and turns it into `def foo` so that we
# don't end up creating the method on the singleton class of the singleton class
# by accident.
#
# This is necessarily done by String manipulation because we can't find out what
# syntax is needed for the argument list by ruby-level introspection.
#
# @param [String] line The original definition line. e.g. def self.foo(bar, baz=1)
# @return [String] The new definition line. e.g. def foo(bar, baz=1)
def definition_for_owner(line)
if line =~ /\Adef (?:.*?\.)?#{Regexp.escape(method.original_name)}(?=[\(\s;]|$)/
"def #{method.original_name}#{$'}"
else
raise CommandError,
"Could not find original `def #{method.original_name}` line " \
"to patch."
end
end
# Apply wrap_for_owner and wrap_for_nesting successively to `source`
# @param [String] source
# @return [String] The wrapped source.
def wrap(source)
wrap_for_nesting(wrap_for_owner(source))
end
# Update the source code so that when it has the right owner when eval'd.
#
# This (combined with definition_for_owner) is backup for the case that
# wrap_for_nesting fails, to ensure that the method will still be defined in
# the correct place.
#View on GitHub (pinned to 1356402628)
Solutions
- Edit the defining file directly: find it with `Foo.instance_method(:price).source_location`, then `edit lib/widget.rb:42`.
- For define_method-created methods, edit the code that generates them, not the method object.
- Re-define manually in the pry buffer instead of using edit-method when the def line is synthetic.
Example fix
# before pry> edit-method Widget#price # CommandError: Could not find original `def price` line to patch. # after pry> Widget.instance_method(:price).source_location # => ['lib/widget.rb', 42] pry> edit lib/widget.rb:42
Defensive patterns
Strategy: fallback
Validate before calling
line = Foo.instance_method(:price).source_location # definition_for_owner needs a literal 'def price' first line; check before patching patchable = line && File.readlines(line[0])[line[1] - 1] =~ /\Adef (?:.*\.)?price\b/
Try / catch
begin
Pry::Method::Patcher.new(method_obj).patch
rescue Pry::CommandError => e
loc = method_obj.source_location
edit(loc.join(':')) if loc # manual fallback
end Prevention
- Prefer `edit <file>:<line>` from source_location over edit-method for metaprogrammed or aliased methods.
- Verify the first source line is a literal def before attempting edit-method.
When it happens
Trigger: `edit-method` on a method created by `define_method(:price) { ... }`; on an aliased method whose original_name points at a different def line; on methods whose source_location line moved because the file was edited after loading.
Common situations: DSL-heavy codebases built on define_method; alias_method wrappers (common in ActiveRecord and decorator gems); hot-reloaded code with stale locations.
Related errors
- Exception has no associated file.
- Cannot edit exceptions raised in REPL.
- No help found for '#{args.first}'
- Must provide a file name.
- Replay index #{index} points out to another replay call: `#{
AI-assisted analysis of pry/pry@1356402628 (2026-08-21).
Data as JSON: /api/errors/8a07eff11c132754.
Report an issue: GitHub.