HomePhorge

(#2782) Fix constant_defined?
3d43d866d662Unpublished

Unpublished Commit ยท Learn More

Repository Importing: This repository is still importing.

Description

(#2782) Fix constant_defined?

Thanks to Al Hoang for the bit of code for choosing how to use
constant_defined? depending on the version of Ruby.

In Ruby 1.9 const_defined? has a new parameter for inherit (from Ruby docs)

mod.const_defined?(sym, inherit=true) -> true or false
Returns true if a constant with the given name is defined by mod, or its
ancestors if inherit is not false.

Unfortunately, the documentation isn't terribly clear about the behavior
if inherit=false. In Ruby 1.8 the inherit parameter doesn't exist. It
appears that setting inherit=false makes it behave like it used to in
Ruby 1.8, but there may be sublties of autoloading that prove this wrong
or ways in which were setting constants that changed and cause problems
regardless of the behavior of const_defined?

Ruby 1.8.7:
irb(main):001:0> module Foo
irb(main):002:1> end

> nil

irb(main):003:0> A = 'find_me?'

> "find_me?"

irb(main):004:0> Foo.const_defined?('A')

> false

Ruby 1.9.2:
ruby-1.9.2-p136 :001 > module Foo
ruby-1.9.2-p136 :002?> end

> nil

ruby-1.9.2-p136 :003 > A = 'find_me?'

> "find_me?"

ruby-1.9.2-p136 :004 > Foo.const_defined?('A')

> true

ruby-1.9.2-p136 :005 > Foo.const_defined?('A', false)

> false

Also noteworthy is that something about constants behavior changed
between 1.9.1 and 1.9.2, though not in regard to the little test above,
but we should only be testing against 1.9.2 anyway.

At least with this change in we'll be able to start debugging test
failures instead of just getting failures at the level of syntax errors.

Reviewed-by: Jacob Helwig <jacob@puppetlabs.com>

Details

Provenance
Matt Robinson <matt@puppetlabs.com>Authored on
vanmeeuwenPushed on Jun 2 2015, 2:22 PM
Parents
rPUad85b67ade99: Merge branch 'feature/next/6527-pip_package_provider' into next
Branches
Unknown
Tags
Unknown

Event Timeline

Matt Robinson <matt@puppetlabs.com> committed rPU3d43d866d662: (#2782) Fix constant_defined? (authored by Matt Robinson <matt@puppetlabs.com>).Mar 23 2011, 9:10 PM