Forum: Ruby const_missing oddity?

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.
unknown (Guest)
on 2006-03-02 04:53
(Received via mailing list)
def Object.const_missing name
  puts "There is no constant: #{name}"
 end

irb(main):038:0> Foo { puts "here!"}
NoMethodError: undefined method `Foo' for main:Object
        from (irb):38
        from :0

irb(main):039:0> Foo
There is no constant: Foo
=> nil

So the block after Foo keeps Foo from being recognized as a constant?
(apparently it's recognized as a method instead)

Phil
unknown (Guest)
on 2006-03-02 05:00
(Received via mailing list)
Hi --

On Thu, 2 Mar 2006, Phil T. wrote:

> irb(main):039:0> Foo
> There is no constant: Foo
> => nil
>
> So the block after Foo keeps Foo from being recognized as a constant?
> (apparently it's recognized as a method instead)

That's what I would expect -- otherwise you'd have a situation where
methods with capitalized names couldn't take blocks.  The same is true
of Foo() with respect to arguments.


David

--
David A. Black (removed_email_address@domain.invalid)
Ruby Power and Light (http://www.rubypowerandlight.com)

"Ruby for Rails" chapters now available
from Manning Early Access Program! http://www.manning.com/books/black
This topic is locked and can not be replied to.