const_defined? not quite in sync with const_get ??

C

Chris Roos

Assuming the following snippet, I'd expect Foo::const_defined?:)Bar)
to be routed via Foo::const_missing. Instead it resolves to top-level
Bar. Can someone explain why please?

--code--

class Bar
end

module Foo
def self.const_missing(konst)
puts "missing konst #{konst}"
end
end

Foo::Bar
#=> missing konst Bar # Unsurprising behaviour

p Foo::const_defined?:)Bar)
false # Unsurprising behaviour

p Foo::const_get:)Bar)
#=> Bar # Surprising behaviour

--/code--

Cheers,

Chris
 
A

ara.t.howard

Assuming the following snippet, I'd expect Foo::const_defined?:)Bar)
to be routed via Foo::const_missing. Instead it resolves to top-level
Bar. Can someone explain why please?

--code--

class Bar
end

module Foo
def self.const_missing(konst)
puts "missing konst #{konst}"
end
end

Foo::Bar
#=> missing konst Bar # Unsurprising behaviour

p Foo::const_defined?:)Bar)
false # Unsurprising behaviour

p Foo::const_get:)Bar)
#=> Bar # Surprising behaviour

--/code--

Cheers,

Chris

non-prefixed constants follow the normal scoping rules:

harp:~ > cat a.rb
class Bar
end

module Foo
def self.const_missing(konst)
puts "missing konst #{konst}"
end
end

Foo::Bar
#=> missing konst Bar # Unsurprising behaviour

p Foo::const_defined?('Bar')
false # Unsurprising behaviour

p Foo::const_get('Foo::Bar')
# ^^^^^
# ^^^^^
# ^^^^^
#=> Bar # Surprising behaviour


harp:~ > ruby a.rb
missing konst Bar
false
a.rb:16:in `const_get': wrong constant name Foo::Bar (NameError)
from a.rb:16


-a
 
C

Chris Roos

non-prefixed constants follow the normal scoping rules:Thanks for the clarification. Purely out of interest, do you find
this behaviour a bit odd (or am I missing obvious reasons as to why it
is implemented this way)?

Chris
 
A

ara.t.howard

Thanks for the clarification. Purely out of interest, do you find
this behaviour a bit odd (or am I missing obvious reasons as to why it
is implemented this way)?

Chris

so that


class C
a = Array.new # up scope
end


and

class C
a = const_get('Array').new
end

behave

i agree it can be confusing, but the alternative, requiring all constants to
be fully scoped, would be an enormous pain in the ass.

cheers.

-a
 
C

Chris Roos

so that


class C
a = Array.new # up scope
end


and

class C
a = const_get('Array').new
end

behave

i agree it can be confusing, but the alternative, requiring all constants to
be fully scoped, would be an enormous pain in the ass.
Ah, of course, that makes sense. Cheers.
 
N

Nobuyoshi Nakada

Date: Thu, 21 Sep 2006 02:06:56 +0900
Posted: Wed, 20 Sep 2006 18:06:54 +0100
From: "Chris Roos" <[email protected]>
Reply-To: (e-mail address removed)
Subject: const_defined? not quite in sync with const_get ??
To: (e-mail address removed) (ruby-talk ML)
Message-Id: <[email protected]>
X-ML-Name: ruby-talk
X-Mail-Count: 215393
 
N

Nobuyoshi Nakada

Hi,

Sorry for mispost.

At Thu, 21 Sep 2006 02:06:56 +0900,
Chris Roos wrote in [ruby-talk:215393]:
Foo::Bar
#=> missing konst Bar # Unsurprising behaviour

p Foo::const_defined?:)Bar)
false # Unsurprising behaviour

p Foo::const_get:)Bar)
#=> Bar # Surprising behaviour

It has changed in 1.9.

p Foo::const_defined?:)Bar) #=> true

p Foo::const_get:)Bar) #=> Bar

p Foo::const_defined?:)Bar, false) #=> false

p Foo::const_get:)Bar, false) #=> missing konst Bar
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top