Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > const_defined? not quite in sync with const_get ??

Reply
Thread Tools

const_defined? not quite in sync with const_get ??

 
 
Chris Roos
Guest
Posts: n/a
 
      09-20-2006
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

 
Reply With Quote
 
 
 
 
ara.t.howard@noaa.gov
Guest
Posts: n/a
 
      09-20-2006
On Thu, 21 Sep 2006, Chris Roos wrote:

> 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
--
in order to be effective truth must penetrate like an arrow - and that is
likely to hurt. -- wei wu wei

 
Reply With Quote
 
 
 
 
Chris Roos
Guest
Posts: n/a
 
      09-20-2006
> 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

 
Reply With Quote
 
ara.t.howard@noaa.gov
Guest
Posts: n/a
 
      09-20-2006
On Thu, 21 Sep 2006, Chris Roos wrote:

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


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
--
in order to be effective truth must penetrate like an arrow - and that is
likely to hurt. -- wei wu wei

 
Reply With Quote
 
Chris Roos
Guest
Posts: n/a
 
      09-20-2006
>
> 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.

 
Reply With Quote
 
Nobuyoshi Nakada
Guest
Posts: n/a
 
      09-21-2006
Date: Thu, 21 Sep 2006 02:06:56 +0900
Posted: Wed, 20 Sep 2006 18:06:54 +0100
From: "Chris Roos" <(E-Mail Removed)>
Reply-To: http://www.velocityreviews.com/forums/(E-Mail Removed)
Subject: const_defined? not quite in sync with const_get ??
To: (E-Mail Removed) (ruby-talk ML)
Message-Id: <3a5e51050609201006j5dc6449cyae74e71099fb3f52@mail .gmail.com>
X-ML-Name: ruby-talk
X-Mail-Count: 215393


2006/9/21, Chris Roos <(E-Mail Removed)>:
> 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
>
>



--
なかだです。

--
--- 僕の前にBugはない。
--- 僕の後ろにBugはできる。
中田 伸悦

 
Reply With Quote
 
Nobuyoshi Nakada
Guest
Posts: n/a
 
      09-21-2006
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

--
Nobu Nakada

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hook, or override const_set, and const_get John Pywtorak Ruby 3 10-03-2007 07:45 AM
constants and const_get Timothy Hunter Ruby 3 08-28-2006 02:07 AM
Problem with Object.const_get? anne001 Ruby 2 07-05-2006 02:57 PM
sync.rb difference between Sync::UN, Sync::EX and Sync::SH Trans Ruby 2 12-12-2005 02:43 PM
Kernel.const_get and gets Rasputin Ruby 0 09-03-2003 03:00 PM



Advertisments