Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NameError: uninitialized constant in a nested for loop #3455

Closed
andrewt0301 opened this issue Nov 11, 2015 · 1 comment
Closed

NameError: uninitialized constant in a nested for loop #3455

andrewt0301 opened this issue Nov 11, 2015 · 1 comment

Comments

@andrewt0301
Copy link

I faced a strange error in JRuby 9.0.3.0. With JRuby 1.7.22, the same code works fine.
A constant gets uninitialized when it is accessed inside a nested for loop. Code is below:

test.rb:

class Base
  @@inherited_classes = Array.new

  def self.inherited_classes
    @@inherited_classes
  end

  def self.inherited(subclass)
    @@inherited_classes.push subclass
  end
end

file = File.expand_path ARGV[0]
require file

inherited_classes = Base::inherited_classes
inherited_classes.each do |inherited_class|
  inherited_class.new
end

test_inherited.rb:

class Inherited < Base
  X = 10

  def initialize
    super
    puts "HI from Inherited.initialize"

    puts X # OK: X is defined here as 10
    for i in 0..1 do
      puts X # OK: X is defined here as 10
      for j in 0..1 do
        puts X # Error: uninitialized constant X
      end
    end
  end
end

Command:
java -jar jruby.jar test.rb test_inherited.rb

Console output:
HI from Inherited.initialize
10
10
NameError: uninitialized constant X
const_missing at org/jruby/RubyModule.java:3181
block in initialize at /home/andrewt/jars/test_inherited.rb:13
each at org/jruby/RubyRange.java:454
block in initialize at /home/andrewt/jars/test_inherited.rb:12
each at org/jruby/RubyRange.java:454
initialize at /home/andrewt/jars/test_inherited.rb:10
block in test.rb at test.rb:19
each at org/jruby/RubyArray.java:1560
at test.rb:18

@kares
Copy link
Member

kares commented Jan 17, 2017

there was a similar issue reported and this is fine on 9.1.6/9.1.7 please feel free to re-open otherwise

@kares kares closed this as completed Jan 17, 2017
@kares kares added this to the Invalid or Duplicate milestone Jan 17, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants