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 <ja...@puppetlabs.com>
Signed-off-by: Matt Robinson <m...@puppetlabs.com>
---
Local-branch: ticket/next/6820_ruby19_fixes
 lib/puppet/util/classgen.rb |   14 +++++++++++++-
 1 files changed, 13 insertions(+), 1 deletions(-)

diff --git a/lib/puppet/util/classgen.rb b/lib/puppet/util/classgen.rb
index ed69c58..1e99aa8 100644
--- a/lib/puppet/util/classgen.rb
+++ b/lib/puppet/util/classgen.rb
@@ -124,11 +124,23 @@ module Puppet::Util::ClassGen
     klass
   end
 
+  # const_defined? in Ruby 1.9 behaves differently in terms
+  # of which class hierarchy it polls for nested namespaces
+  #
+  # See http://redmine.ruby-lang.org/issues/show/1915
+  def is_constant_defined?(const)
+    if ::RUBY_VERSION =~ /1.9/
+      const_defined?(const, false)
+    else
+      const_defined?(const)
+    end
+  end
+
   # Handle the setting and/or removing of the associated constant.
   def handleclassconst(klass, name, options)
     const = genconst_string(name, options)
 
-    if const_defined?(const)
+    if is_constant_defined?(const)
       if options[:overwrite]
         Puppet.info "Redefining #{name} in #{self}"
         remove_const(const)
-- 
1.7.3.1

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Developers" group.
To post to this group, send email to puppet-dev@googlegroups.com.
To unsubscribe from this group, send email to 
puppet-dev+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/puppet-dev?hl=en.

Reply via email to