On Nov 25, 12:54=A0pm, Josh Cheek <josh.ch... / gmail.com> wrote:
> On Thu, Nov 25, 2010 at 7:54 AM, Intransition <transf... / gmail.com> wrote=
:
> > Ruby's VERSION constant is getting in the way of using #const_missing
> > in my module. Here's some example code:
>
> > =A0module Foo
> > =A0 =A0def self.const_data
> > =A0 =A0 =A0@const_data ||=3D { 'version' =3D> '1.0.0' }
> > =A0 =A0end
>
> > =A0 =A0def self.const_missing(name)
> > =A0 =A0 =A0key =3D name.to_s.downcase
> > =A0 =A0 =A0const_data[key] || super(name)
> > =A0 =A0end
> > =A0end
>
> > =A0Foo::VERSION =A0#=3D> '1.8.7'
>
> > How can I fix this?
>
> > I tried `remove_const(:VERSION)` at the toplevel but discovered that
> > some Ruby library don't like that (sorry, can't recall which it was
> > off hand).
>
> Here are my results.
>
> module Foo
> =A0 def self.const_data
> =A0 =A0 @const_data ||=3D { 'version' =3D> '1.0.0' }
> =A0 end
>
> =A0 def self.const_missing(name)
> =A0 =A0 key =3D name.to_s.downcase
> =A0 =A0 const_data[key] || super(name)
> =A0 end
>
> =A0 VERSION =A0 =A0 =A0 # =3D> "1.8.7"
> =A0 self::VERSION # =3D> "1.0.0"
>
> =A0 def self.method
> =A0 =A0 VERSION =A0 =A0 # =3D> "1.8.7"
> =A0 end
> =A0 method
> end
>
> # going back through ruby versions
> # I wasn't able to get these to print
> # the ruby version until 1.7.1
> # thanks, ruby-versions.net :)
> Foo::VERSION # =3D> "1.0.0"
> Foo::VERSION # =3D> "1.0.0"

Hmmm... I haven't been able to quite get the rhyme or reason of the
behavior, but I just got the error again. The code is:

  class TracePoint

    # Access to metadata.
    def self.metadata
      @metadata ||=3D (
        require 'yaml'
        YAML.load(File.new(File.dirname(__FILE__) + '/
tracepoint.yml'))
      )
    end

    # Access metadata as constants.
    def self.const_missing(name)
      name =3D name.to_s
      metadata[name] || super(name)
    end

When I use run:

  require 'tracepoint'
  TracePoint::VERSION

I get:

tryit.rb:2: warning: toplevel constant VERSION referenced by
TracePoint::VERSION
"1.8.7"

I got this on both:
  ruby 1.8.7 (2010-01-10 patchlevel 249) [x86_64-linux]
  ruby 1.8.7 (2010-08-16 patchlevel 302) [x86_64-linux]

Ruby 1.9.2 is not a problem.