From: "mame (Yusuke Endoh)" Date: 2022-07-20T02:48:22+00:00 Subject: [ruby-core:109254] [Ruby master Feature#18913] Add object name to the NoMethodError error message: undefined method `_method_' for `_class_' in `_object_name_' Issue #18913 has been updated by mame (Yusuke Endoh). Status changed from Open to Rejected I close this ticket. If you have any opinion, please let me know. ---------------------------------------- Feature #18913: Add object name to the NoMethodError error message: undefined method `_method_' for `_class_' in `_object_name_' https://bugs.ruby-lang.org/issues/18913#change-98386 * Author: stillhart (Fabian Stillhart) * Status: Rejected * Priority: Normal ---------------------------------------- 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: