Error message, hide class name

T

Trans

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.
 
T

Trans

irb(main):005:0> class X
irb(main):006:1> =A0 def inspect
irb(main):007:2> =A0 =A0 "hey"
irb(main):008:2> =A0 end
irb(main):009:1> end
=3D> nil
irb(main):010:0> x =3D X.new.nomethod
NoMethodError: undefined method `nomethod' for hey:X
=A0 =A0 =A0 =A0 from (irb):10

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

=A0 class X
=A0 =A0 def self.name; ''; end
=A0 =A0 def self.to_s; ''; end
=A0 =A0 def class; ''; end
=A0 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>
 
P

Pit Capitain

2008/11/19 Trans said:
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
 
R

Ryan Davis

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:

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:
 
T

Trans

the problem isn't inspect or anything else to do with X, it is =A0
method_missing. the default method_missing is raising with a message =A0
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:

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

Thanks, Ryan.

T.
 
T

Trans

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:

=A0 class X
=A0 =A0 def inspect
=A0 =A0 =A0 "#hey"
=A0 =A0 end
=A0 end

=A0 X.new.x =A0# =3D> undefined method `x' for #hey (NoMethodError)

That's interesting!

Thanks Pit. I think I can use that.

T.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top