VERSION constant issue

I

Intransition

Ruby's VERSION constant is getting in the way of using #const_missing
in my module. Here's some example code:

module Foo
def self.const_data
@const_data ||= { 'version' => '1.0.0' }
end

def self.const_missing(name)
key = name.to_s.downcase
const_data[key] || super(name)
end
end

Foo::VERSION #=> '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).
 
R

Robert Klemme

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

Is this really an issue with ::VERSION? To me this rather looks like
an issue with lookup logic (either the general or yours) since the
same would apply to all other constants, wouldn't it?

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
A

ara.t.howard

hrm...


cfp:~ > cat a.rb
module Lib
def Lib.const_data
@const_data ||= {
'version' => '4.2.0'
}
end

def Lib.const_missing(const)
const_data[const.to_s.downcase] || super
end
end

p Lib::VERSION



cfp:~ > rvm a.rb

info: ruby-1.8.7-p302: ruby 1.8.7 (2010-08-16 patchlevel 302) [i686-
darwin9.8.0]

"4.2.0"

info: ruby-1.9.2-p0: ruby 1.9.2p0 (2010-08-18 revision 29036) [i386-
darwin9.8.0]

"4.2.0"
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

Ruby's VERSION constant is getting in the way of using #const_missing
in my module. Here's some example code:

module Foo
def self.const_data
@const_data ||= { 'version' => '1.0.0' }
end

def self.const_missing(name)
key = name.to_s.downcase
const_data[key] || super(name)
end
end

Foo::VERSION #=> '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
def self.const_data
@const_data ||= { 'version' => '1.0.0' }
end

def self.const_missing(name)
key = name.to_s.downcase
const_data[key] || super(name)
end

VERSION # => "1.8.7"
self::VERSION # => "1.0.0"

def self.method
VERSION # => "1.8.7"
end
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 # => "1.0.0"
Foo::VERSION # => "1.0.0"
 
I

Intransition

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

Intransition

Is this really an issue with ::VERSION? =A0To me this rather looks like
an issue with lookup logic (either the general or yours) since the
same would apply to all other constants, wouldn't it?

Yes, it would be.
 
E

Eric Hodel

Ruby's VERSION constant is getting in the way of using #const_missing
in my module. Here's some example code:
=20
module Foo
def self.const_data
@const_data ||=3D { 'version' =3D> '1.0.0' }
end
=20
def self.const_missing(name)
key =3D name.to_s.downcase
const_data[key] || super(name)
end
end
=20
Foo::VERSION #=3D> '1.8.7'
=20
How can I fix this?

What would ::const_data provide that Module#constants, Module#const_get =
and Module#const_set and direct constant access don't provide? If it's =
something legitimate, invert ::const_data to reference real constants =
via introspection and get rid of const_missing.
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).

File a bug, they should be using RUBY_VERSION.=
 
I

Intransition

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?

What would ::const_data provide that Module#constants, Module#const_get a=
nd Module#const_set and direct constant access don't provide? =A0If it's so=
mething legitimate, invert ::const_data to reference real constants via int=
rospection and get rid of const_missing.

I could, but I wanted to lazy load the information b/c it is being
read from a file, and in most usecases will not be used. The reason I
am using constants is simply b/c it's customary in this case as the
information is project metadata, such as VERSION.
File a bug, they should be using RUBY_VERSION.

Will do. I tracked it down to a Ruby 1.8.7:

e2mmap.rb:fail "Use Ruby 1.1" if VERSION < "1.1"
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top