{"record":{"id":"8a07eff11c132754","repo":"pry/pry","slug":"could-not-find-original-def-method-original-nam","errorCode":null,"errorMessage":"Could not find original `def #{method.original_name}` line to patch.","messagePattern":"Could not find original `def #(.+?)` line to patch\\.","errorType":"exception","errorClass":"CommandError","httpStatus":null,"severity":"error","filePath":"lib/pry/method/patcher.rb","lineNumber":82,"sourceCode":"      end\n\n      # Update the definition line so that it can be eval'd directly on the Method's\n      # owner instead of from the original context.\n      #\n      # In particular this takes `def self.foo` and turns it into `def foo` so that we\n      # don't end up creating the method on the singleton class of the singleton class\n      # by accident.\n      #\n      # This is necessarily done by String manipulation because we can't find out what\n      # syntax is needed for the argument list by ruby-level introspection.\n      #\n      # @param [String] line The original definition line. e.g. def self.foo(bar, baz=1)\n      # @return [String]  The new definition line. e.g. def foo(bar, baz=1)\n      def definition_for_owner(line)\n        if line =~ /\\Adef (?:.*?\\.)?#{Regexp.escape(method.original_name)}(?=[\\(\\s;]|$)/\n          \"def #{method.original_name}#{$'}\"\n        else\n          raise CommandError,\n                \"Could not find original `def #{method.original_name}` line \" \\\n                \"to patch.\"\n        end\n      end\n\n      # Apply wrap_for_owner and wrap_for_nesting successively to `source`\n      # @param [String] source\n      # @return [String] The wrapped source.\n      def wrap(source)\n        wrap_for_nesting(wrap_for_owner(source))\n      end\n\n      # Update the source code so that when it has the right owner when eval'd.\n      #\n      # This (combined with definition_for_owner) is backup for the case that\n      # wrap_for_nesting fails, to ensure that the method will still be defined in\n      # the correct place.\n      #","sourceCodeStart":64,"sourceCodeEnd":100,"githubUrl":"https://github.com/pry/pry/blob/135640262879544c6bfecbf3e78511289bfe956c/lib/pry/method/patcher.rb#L64-L100","documentation":"`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.","triggerScenarios":"`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.","commonSituations":"DSL-heavy codebases built on define_method; alias_method wrappers (common in ActiveRecord and decorator gems); hot-reloaded code with stale locations.","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."],"exampleFix":"# before\npry> edit-method Widget#price\n# CommandError: Could not find original `def price` line to patch.\n\n# after\npry> Widget.instance_method(:price).source_location # => ['lib/widget.rb', 42]\npry> edit lib/widget.rb:42","handlingStrategy":"fallback","validationCode":"line = Foo.instance_method(:price).source_location\n# definition_for_owner needs a literal 'def price' first line; check before patching\npatchable = line && File.readlines(line[0])[line[1] - 1] =~ /\\Adef (?:.*\\.)?price\\b/","typeGuard":null,"tryCatchPattern":"begin\n  Pry::Method::Patcher.new(method_obj).patch\nrescue Pry::CommandError => e\n  loc = method_obj.source_location\n  edit(loc.join(':')) if loc # manual fallback\nend","preventionTips":["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."],"tags":["pry","ruby","edit-method","monkey-patching","source-rewriting"],"backgroundTag":"method-patch-failed","analyzedSha":"135640262879544c6bfecbf3e78511289bfe956c","analyzedAt":"2026-08-21T19:07:03.985Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}