Jruby: Refinement with `super` vs. inheritance

Created on 17 Jun 2018  路  3Comments  路  Source: jruby/jruby

Environment

  • JRuby version: jruby 9.2.0.0 (2.5.0) 2018-05-24 81156a8 OpenJDK 64-Bit Server VM 25.171-b11 on 1.8.0_171-8u171-b11-1~deb9u1-b11 +jit [linux-x86_64]
  • Operating system and platform: Linux 3971953e9581 4.9.49-moby #1 SMP Fri Dec 8 13:40:02 UTC 2017 x86_64 GNU/Linux

Expected Behavior

The code below print "foo-o-o" (as in CRuby):

class A
  def foo
    "foo"
  end
end

class B < A; end

module SuperExt
  refine A do
    def foo
      super + "-o-o"
    end
  end
end

using SuperExt

p B.new.foo

Actual Behavior

It doesn't activate the refinement and prints "foo".

When calling on a A class instance works as expected:

class A
  def foo
    "foo"
  end
end

module SuperExt
  refine A do
    def foo
      super + "-o-o"
    end
  end
end

using SuperExt

p A.new.foo #=> "foo-o-o"

Most helpful comment

Both PRs and additional fixes have been merged.

All 3 comments

This means that you can't refine Numeric for example:-

module SuperNumeric 
  refine Numeric do
    def degrees
      self * 57.29577951308232
    end

    def radians
      self * 0.017453292519943295
    end
  end
end

using SuperNumeric

puts 45.radians
puts 0.7853981634.degrees

mri ruby:-

0.7853981633974483
45.0000000001462

jruby (9.2.0.0 (2.5.0) 2018-05-24 81156a8 OpenJDK 64-Bit Server VM 25.171-b11 on 1.8.0_171-8u171-b11-0ubuntu0.16.04.1-b11 +jit [linux-x86_64])

NoMethodError: undefined method `radians' for 45:Integer
  <main> at numeric.rb:15

This is fixed after #5604 and #5627.

Both PRs and additional fixes have been merged.

Was this page helpful?
0 / 5 - 0 ratings