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

Refinement is not activated within itself #5054

Closed
palkan opened this issue Feb 18, 2018 · 3 comments
Closed

Refinement is not activated within itself #5054

palkan opened this issue Feb 18, 2018 · 3 comments
Milestone

Comments

@palkan
Copy link

palkan commented Feb 18, 2018

Environment

  • JRuby version
jruby 9.1.15.0 (2.3.3) 2017-12-07 929fde8 OpenJDK 64-Bit Server VM 25.151-b12 on 1.8.0_151-8u151-b12-1~deb9u1-b12 +jit [linux-x86_64]

Expected Behavior

Given a simple script:

module DeepDup
  refine ::Hash do
    def deep_dup
      each_with_object(dup) do |(key, value), hash|
        hash[key] = if value.is_a?(::Hash) || value.is_a?(::Array)
                      value.deep_dup
                    else
                      value
                    end
      end
    end
  end
end

module TestRefinement
  using DeepDup

  def self.run
    source = {
      a: 1,
      b: 'hello',
      c: {
        id: 1,
        data: { name: 'John' }
      }
    }

    source.deep_dup
  end
end

TestRefinement.run

puts "All OK"

I expect it to print "All OK" (that's MRI behaviour).

Actual Behavior

NoMethodError: undefined method `deep_dup' for {:id=>1, :data=>{:name=>"John"}}:Hash
  block in deep_dup at refinement_bug.rb:6
               each at org/jruby/RubyHash.java:1343
   each_with_object at org/jruby/RubyEnumerable.java:1077
           deep_dup at refinement_bug.rb:4
                run at refinement_bug.rb:28
             <main> at refinement_bug.rb:32
@palkan
Copy link
Author

palkan commented Feb 19, 2018

UPD: Adding using self right after refine ... do .. end make it work as expected (though it looks strange–refinements should only affect everything below using).

@headius
Copy link
Member

headius commented Feb 20, 2018

In JRuby we use the presence of a call to "using" to flip on refinement mode for a given scope, so we're not checking for refinements all the time. We should probably be doing the same thing for the block passed to refine. cc @enebo

@headius
Copy link
Member

headius commented Feb 26, 2019

The example script works as expected once #5627 lands, probably a combination of this work and the refinement refit in #5604.

@headius headius closed this as completed Mar 13, 2019
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