Forum: Ruby Error message, hide class name

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Thomas S. (Guest)
on 2008-11-19 18:54
(Received via mailing list)
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.
Thomas S. (Guest)
on 2008-11-19 20:27
(Received via mailing list)
On Nov 19, 11:50 am, Trans <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<Class>
Pit C. (Guest)
on 2008-11-20 10:52
(Received via mailing list)
2008/11/19 Trans <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
Ryan D. (Guest)
on 2008-11-20 11:33
(Received via mailing list)
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:
Thomas S. (Guest)
on 2008-11-20 11:58
(Received via mailing list)
On Nov 20, 4:29 am, Ryan D. <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.
Thomas S. (Guest)
on 2008-11-20 12:00
(Received via mailing list)
On Nov 20, 3:48 am, "Pit C." <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.
This topic is locked and can not be replied to.