[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

ri strangeness

martinus

2/16/2005 8:27:00 AM

Hi! I am currently writing a simple FXRuby GUI for RI (soon to be
available on rubyforge, if it gets approoved :-). I have noticed some
strange behaviours, which can be easily reproduced like this:

type 'ri !': information for NameError::message::! shows up.

type 'ri message::!' Bad argument: message::!

How does one define a method called just '!' ?
And how can it be 'message ' and not Message'?
strange, strange...

martinus

1 Answer

Florian Gross

2/16/2005 9:14:00 PM

0

martinus wrote:

> Hi! I am currently writing a simple FXRuby GUI for RI (soon to be
> available on rubyforge, if it gets approoved :-). I have noticed some
> strange behaviours, which can be easily reproduced like this:
>
> type 'ri !': information for NameError::message::! shows up.
>
> type 'ri message::!' Bad argument: message::!
>
> How does one define a method called just '!' ?

Class.new { define_method(:"!") { ... } }

> And how can it be 'message ' and not Message'?
> strange, strange...

To do that kind of trickery you need to be at the C level. Ruby
sometimes needs to create modules and classes that are not visible to
your average Ruby application. In those cases it usually uses invalid
names. (There's also the 'fatal' exception, for example.)

Nevertheless, we can still get our hands on those special objects if we
try hard enough:

irb(main):002:0> result = nil; ObjectSpace.each_object { |obj| result =
obj if obj.inspect == "NameError::message" }
=> 20510
irb(main):003:0> result
=> NameError::message
irb(main):004:0> result.method(:"!")
=> #<Method: NameError::message.!>

We can even call it:

irb(main):010:0> result.send(:"!", 1, 1, 1)
=> #<NameError::message:0x2b966d0>

Nothing funky happens, though.

BTW, that method and class probably shouldn't be documented.