From: "stillhart (Fabian Stillhart)" Date: 2022-07-14T08:04:11+00:00 Subject: [ruby-core:109203] [Ruby master Bug#18913] Add object name to the NoMethodError error message: undefined method `_method_' for `_class_' in `_object_name_' Issue #18913 has been reported by stillhart (Fabian Stillhart). ---------------------------------------- Bug #18913: Add object name to the NoMethodError error message: undefined method `_method_' for `_class_' in `_object_name_' https://bugs.ruby-lang.org/issues/18913 * Author: stillhart (Fabian Stillhart) * Status: Open * Priority: Normal * ruby -v: ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [x86_64-linux] * Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN ---------------------------------------- Hi I think the `NoMethodError` error message is missing the object name. The object name is already present in `NameError`. Currently one needs to dig into the source code and debug it in order to find which variable or method is actually affected. The current error message looks like this: ``` ruby bar = nil bar.i_wish_i_saw_the_name_bar # (irb):00:in `
': undefined method `i_wish_i_saw_the_name_bar' for nil:NilClass (NoMethodError) ``` I wish that it was more like this ``` ruby bar = nil bar.i_wish_i_saw_the_name_bar #(irb):00:in `
': undefined method `i_wish_i_saw_the_name_bar' for nil:NilClass in `bar' (NoMethodError) # ^^^ It should mention the object name ``` The NameError error message supports this already, and I think it avoids a whole lot of issue tickets, stackoverflow searches and more. See NoMethodError vs NameError https://trends.google.ch/trends/explore?date=today%205-y&q=ruby%20%20NoMethodError,ruby%20NameError ``` ruby foo # (irb):00:in `
': undefined local variable or method `foo' for main:Object (NameError) # ^^^ the object name is mentioned ``` One can always use the backtrace to find the affected line, but even that won't help in the following situation: ```ruby a = OpenStruct.new b = nil c = nil who_failed = a.foo & b.foo & c.foo # (irb):00:in `
': undefined method `foo' for nil:NilClass (NoMethodError) ``` I would add the object name at the end of the exception message not to break compatibility. I hope most users use regex to match the exception message. I don't know how to program c but I think the source is located here https://github.com/ruby/ruby/blob/554befb/proc.c#L1959 I assume there will be some edge cases around meta programming and procs. Anyway, I think we should show them if available. I think we all spent many hours tracking down when we have a type/method mismatch, and this would have helped greatly. -- https://bugs.ruby-lang.org/ Unsubscribe: