const_defined? not quite in sync with const_get ??

Discussion in 'Ruby' started by Chris Roos, Sep 20, 2006.

  1. Chris Roos

    Chris Roos Guest

    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
     
    Chris Roos, Sep 20, 2006
    #1
    1. Advertising

  2. Chris Roos

    Guest

    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
     
    , Sep 20, 2006
    #2
    1. Advertising

  3. Chris Roos

    Chris Roos Guest

    > 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
     
    Chris Roos, Sep 20, 2006
    #3
  4. Chris Roos

    Guest

    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
     
    , Sep 20, 2006
    #4
  5. Chris Roos

    Chris Roos Guest

    >
    > 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.
     
    Chris Roos, Sep 20, 2006
    #5
  6. Date: Thu, 21 Sep 2006 02:06:56 +0900
    Posted: Wed, 20 Sep 2006 18:06:54 +0100
    From: "Chris Roos" <>
    Reply-To:
    Subject: const_defined? not quite in sync with const_get ??
    To: (ruby-talk ML)
    Message-Id: <>
    X-ML-Name: ruby-talk
    X-Mail-Count: 215393


    2006/9/21, 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
    >
    >



    --
    なかだです。

    --
    --- 僕の前にBugはない。
    --- 僕の後ろにBugはできる。
    中田 伸悦
     
    Nobuyoshi Nakada, Sep 21, 2006
    #6
  7. 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
     
    Nobuyoshi Nakada, Sep 21, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rasputin

    Kernel.const_get and gets

    Rasputin, Sep 3, 2003, in forum: Ruby
    Replies:
    0
    Views:
    107
    Rasputin
    Sep 3, 2003
  2. Marcin Mielyski

    const_defined? raises NameError ?

    Marcin Mielyski, Aug 7, 2004, in forum: Ruby
    Replies:
    4
    Views:
    179
    Marcin Mielyski
    Aug 7, 2004
  3. Kevin Howe

    const_defined? and inheritance

    Kevin Howe, Feb 12, 2005, in forum: Ruby
    Replies:
    4
    Views:
    94
    Joel VanderWerf
    Feb 12, 2005
  4. Trans
    Replies:
    2
    Views:
    485
    Trans
    Dec 12, 2005
  5. Shak

    const_defined?

    Shak, Dec 4, 2009, in forum: Ruby
    Replies:
    1
    Views:
    78
Loading...

Share This Page