Skip to content

it special block variable hides local variable sometimes #8767

@ccutrer

Description

@ccutrer
#!/usr/bin/env ruby

class A
  def shift(count)
    it = "hi"
    puts it.inspect
    count.times do
      puts it.inspect
      break unless it.length == 1
    end
    "bye"
  end
end

A.new.shift(3)

Environment Information

jruby 10.0.0.0 (3.4.2) 2025-04-13 6ed59bc OpenJDK 64-Bit Server VM 21.0.6+7-Ubuntu-124.04.1 on 21.0.6+7-Ubuntu-124.04.1 +indy +jit [x86_64-linux]

Expected Behavior

Expected output (and actual output from JRuby 9.4.12.0 and CRuby 3.4.2):

"hi"
"hi"

Actual Behavior

Actual output:

"hi"
0
NoMethodError: undefined method 'length' for an instance of Integer
   shift at ./test.rb:9
   times at org/jruby/RubyFixnum.java:343
   shift at ./test.rb:7
  <main> at ./test.rb:15

It seems that it in the block is referring to the special block variable, even though a local of the same name already exists (which apparently makes the special block variable not created in CRuby?)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions