# Error message, hide class name

**URL:** <https://www.ruby-forum.com/t/error-message-hide-class-name/153641>\
**Category:** Ruby\
**Created:** [November 19, 2008, 5:54pm UTC](https://www.ruby-forum.com/t/error-message-hide-class-name/153641 "2008-11-19T17:54:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [November 19, 2008, 5:54pm UTC](https://www.ruby-forum.com/t/error-message-hide-class-name/153641/1 "2008-11-19T17:54:31Z")

</div>

irb(main):005:0\> class X  
irb(main):006:1\> def inspect  
irb(main):007:2\> “hey”  
irb(main):008:2\> end  
irb(main):009:1\> end  
=\> nil  
irb(main):010:0\> x = X.new.nomethod  
NoMethodError: undefined method `nomethod’ for hey:X  
from (irb):10

Anyone know how I can get rid of the ‘:X’ after ‘hey’? I’ve tried:

class X  
def self.name; ‘’; end  
def self.to\_s; ‘’; end  
def class; ‘’; end  
end

Where is it getting the class name?

Thanks,  
T.

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [November 19, 2008, 7:27pm UTC](https://www.ruby-forum.com/t/error-message-hide-class-name/153641/2 "2008-11-19T19:27:21Z")

</div>

On Nov 19, 11:50 am, Trans [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Anyone know how I can get rid of the ‘:X’ after ‘hey’? I’ve tried:
> 
> class X  
> def self.name; ‘’; end  
> def self.to\_s; ‘’; end  
> def class; ‘’; end  
> end
> 
> Where is it getting the class name?

FYI, I decided to just chomp off everything after the ‘for’, so no big  
deal. But I still don’t understand where the error message was getting  
the class name.

T

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [November 20, 2008, 10:33am UTC](https://www.ruby-forum.com/t/error-message-hide-class-name/153641/3 "2008-11-20T10:33:51Z")

</div>

On Nov 19, 2008, at 08:50 , Trans wrote:

> Anyone know how I can get rid of the ‘:X’ after ‘hey’? I’ve tried:  
> the problem isn’t inspect or anything else to do with X, it is  
> method\_missing. the default method\_missing is raising with a message  
> like:

“undefined method `#{msg}’ for #{self.inspect}:#{self.class}”

So you can attack the ‘problem’ at the source:

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [November 20, 2008, 9:52am UTC](https://www.ruby-forum.com/t/error-message-hide-class-name/153641/4 "2008-11-20T09:52:42Z")

</div>

2008/11/19 Trans [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> But I still don’t understand where the error message was getting  
> the class name.

Tom, the so-called class path seems to be buried deep inside  
unaccessible instance variables of the class. For a workaround use a  
“#” as the first character of the inspect result:

class X  
def inspect  
“#hey”  
end  
end

X.new.x # =\> undefined method `x’ for #hey (NoMethodError)

Regards,  
Pit

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [November 20, 2008, 11:00am UTC](https://www.ruby-forum.com/t/error-message-hide-class-name/153641/5 "2008-11-20T11:00:36Z")

</div>

On Nov 20, 3:48 am, “Pit C.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> def inspect
> "#hey"
> end
> 
> ```
> 
> end
> 
> X.new.x # =\> undefined method `x’ for #hey (NoMethodError)

That’s interesting!

Thanks Pit. I think I can use that.

T.

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [November 20, 2008, 10:58am UTC](https://www.ruby-forum.com/t/error-message-hide-class-name/153641/6 "2008-11-20T10:58:49Z")

</div>

On Nov 20, 4:29 am, Ryan D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Anyone know how I can get rid of the ‘:X’ after ‘hey’? I’ve tried:
> 
> the problem isn’t inspect or anything else to do with X, it is  
> method\_missing. the default method\_missing is raising with a message  
> like:
> 
> “undefined method `#{msg}’ for #{self.inspect}:#{self.class}”  
> ^^^^^^^^^^  
> Which is why I thought I should be able to override #class. And was  
> surprised that it did not work. Nevertheless…

> So you can attack the ‘problem’ at the source:
> 
> > > > class Object; def method\_missing(\*a); raise NoMethodError, “haha!  
> > > > stupid!”; end; end  
> > > > =\> nil  
> > > > bork  
> > > > NoMethodError: haha! stupid!  
> > > > from (irb):3:in `method\_missing’  
> > > > from (irb):4

That’s a better approach --at least in the case of NoMethodError.

Thanks, Ryan.

T.
