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

Refinements do not refine #4330

Closed
zverok opened this issue Nov 22, 2016 · 15 comments
Closed

Refinements do not refine #4330

zverok opened this issue Nov 22, 2016 · 15 comments
Milestone

Comments

@zverok
Copy link

zverok commented Nov 22, 2016

Environment

$ ruby -v
jruby 9.1.6.0 (2.3.1) 2016-11-22 0150a76 OpenJDK Server VM 24.65-b04 on 1.7.0_65-b32 +jit [linux-i386]
$ uname -a
Linux platypus 3.13.0-37-generic #64-Ubuntu SMP Mon Sep 22 21:30:01 UTC 2014 i686 i686 i686 GNU/Linux

Expected Behavior

module Refinements
  refine Object do
    def derp
      yield self
    end
  end
end

module A
  using Refinements

  def self.foo
    'test'.derp { |s| '(' + s + ')' }
  end
end

p A.foo # => "(test)"

Works in any MRI Ruby 2.1+

Actual Behavior

p A.foo
#   NoMethodError: undefined method `derp' for "test":String
#        foo at tmp/ref.rb:13
#     <main> at tmp/ref.rb:17

Note

I believe, as this example is VERY basic and nobody have complained about the behavior, the refinements are really not used in community... Or JRuby users are used to not rely on them, at least. So, it is hard to estimate how severe the bug should be considered.

@najamelan
Copy link

Isn't this the same as #3548?

@zverok
Copy link
Author

zverok commented Feb 4, 2017

Yep, looks alike, yet that one marked as closed. And mine is still actual.

@pan
Copy link

pan commented Mar 30, 2017

The issue is still on v9.1.8.0

kputnam added a commit to kputnam/stupidedi that referenced this issue Oct 18, 2017
It seems like JRuby doesn't correctly implement refinements see
(jruby/jruby#4330) so the only way around this was to revert to
monkey-patching the base classes. New file lib/ruby/jruby_hack.rb
is copy-pasted code from each lib/ruby file, except instead of
'refine X do { ... }' it's changed to 'class X { ... }'.

There was also an incompatibility with Nn.to_d - Nn, which fails
with "Nn cannot be coerced to BigDecimal"; this might be an error
in the BigDecimal class on JRuby? For now, those specs are marked
pending on JRuby.

Redcarpet is a C-extension for MRI, so I disabled it for JRuby,
but documentation can be accessed online (just not built on JRuby)
@zverok
Copy link
Author

zverok commented Mar 31, 2018

Can this fact at least be mentioned and explained in Wiki? https://github.com/jruby/jruby/wiki/DifferencesBetweenMriAndJruby

I know there are different views on refinements, but it is a major language feature that is just silently ignored by JRuby for literally years now.

@headius
Copy link
Member

headius commented Apr 12, 2018

I understand this has been frustrating. I'm going to pin this to my todo list and try to get refinements working better for 9.2, which we hope to release in about a month.

The wiki is publicly editable...please feel free to add refinements as a topic on that page.

@andy-twosticks
Copy link

This does indeed appear to be fixed in 9.2.0.0 (I came across it when trying to use the Origami gem).

@headius
Copy link
Member

headius commented Jun 13, 2018

Hey, that's one issue down! Thanks for verifying.

@headius headius closed this as completed Jun 13, 2018
@headius headius modified the milestones: JRuby 9.3.0.0, JRuby 9.2.0.0 Jun 13, 2018
@zverok
Copy link
Author

zverok commented Jun 13, 2018

I believe I've checked that with 9.2.0.0. It was working on different classes, but not with Object (like in the example in the issue). Will check.

@zverok
Copy link
Author

zverok commented Jun 13, 2018

Sorry :(

$ bin/jruby test.rb
"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.17.10.1-b11 +jit [linux-x86_64]"
NoMethodError: undefined method `derp' for "test":String
     foo at test.rb:15
  <main> at test.rb:19

test.rb:

p RUBY_DESCRIPTION

module Refinements
  refine Object do
    def derp
      yield self
    end
  end
end

module A
  using Refinements

  def self.foo
    'test'.derp { |s| '(' + s + ')' }
  end
end

p A.foo # => "(test)"

With my regular Ruby:

"ruby 2.4.2p198 (2017-09-14 revision 59899) [x86_64-linux]"
"(test)"

Probaby the problem now is much more local than "refinements do not work at all".

@ghost
Copy link

ghost commented Jul 9, 2018

I too have a breaking case in 9.2.0.0.

It isn't quite the same as zverok's.

p RUBY_DESCRIPTION

class Foo
end

class Bar < Foo
end

module FooRef
  refine Foo do
    def burp
      puts "burp"
    end
  end
end

using FooRef
Bar.new.burp

jruby:

"jruby 9.2.0.0 (2.5.0) 2018-05-24 81156a8 Java HotSpot(TM) 64-Bit Server VM 25.66-b17 on 1.8.0_66-b17 +jit [darwin-x86_64]"
NoMethodError: undefined method `burp' for #<Bar:0x6ee52dcd>
  <main> at fooref.rb:18

ruby:

"ruby 2.5.0p0 (2017-12-25 revision 61468) [x86_64-darwin15]"
burp

@p-mongo
Copy link

p-mongo commented Dec 27, 2018

It looks like i18n 1.3.0 is not usable on jruby 9.1 and 9.2 due to refinements.

i18n has this code:

module I18n
  module HashRefinements
    refine Hash do
      def slice(*keep_keys)
        h = {}
        keep_keys.each { |key| h[key] = fetch(key) if has_key?(key) }
        h
      end

      def except(*less_keys)
        slice(*keys - less_keys)
      end

      def deep_symbolize_keys
        each_with_object({}) do |(key, value), result|
          value = value.deep_symbolize_keys if value.is_a?(Hash)
          result[symbolize_key(key)] = value
          result
        end
      end

      # deep_merge_hash! by Stefan Rusterholz, see http://www.ruby-forum.com/topic/142809
      def deep_merge!(data)
        merger = lambda do |_key, v1, v2|
          Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2
        end
        merge!(data, &merger)
      end

      private

      def symbolize_key(key)
        key.respond_to?(:to_sym) ? key.to_sym : key
      end
    end
  end
end

And this produces the following error in Mongoid test suite:

  1) Mongoid::Criteria#asc when the documents are embedded returns the sorted documents
     Failure/Error: field :_id, type: String, overwrite: true, default: ->{ street.try(:parameterize) }
     
     NoMethodError:
       undefined method `symbolize_key' for #
     # /home/w/.rbenv/versions/jruby-9.2.0.0/lib/ruby/gems/shared/gems/i18n-1.3.0/lib/i18n/core_ext/hash.rb:17:in `block in deep_symbolize_keys'
     # org/jruby/RubyHash.java:1396:in `each'
     # org/jruby/RubyEnumerable.java:1190:in `each_with_object'
     # /home/w/.rbenv/versions/jruby-9.2.0.0/lib/ruby/gems/shared/gems/i18n-1.3.0/lib/i18n/core_ext/hash.rb:15:in `deep_symbolize_keys'
     # /home/w/.rbenv/versions/jruby-9.2.0.0/lib/ruby/gems/shared/gems/i18n-1.3.0/lib/i18n/backend/simple.rb:46:in `store_translations'
     # /home/w/.rbenv/versions/jruby-9.2.0.0/lib/ruby/gems/shared/gems/i18n-1.3.0/lib/i18n/backend/base.rb:220:in `block in load_file'
     # org/jruby/RubyHash.java:1396:in `each'
     # /home/w/.rbenv/versions/jruby-9.2.0.0/lib/ruby/gems/shared/gems/i18n-1.3.0/lib/i18n/backend/base.rb:220:in `load_file'
     # /home/w/.rbenv/versions/jruby-9.2.0.0/lib/ruby/gems/shared/gems/i18n-1.3.0/lib/i18n/backend/base.rb:18:in `block in load_translations'
     # org/jruby/RubyArray.java:1801:in `each'
     # /home/w/.rbenv/versions/jruby-9.2.0.0/lib/ruby/gems/shared/gems/i18n-1.3.0/lib/i18n/backend/base.rb:18:in `load_translations'
     # /home/w/.rbenv/versions/jruby-9.2.0.0/lib/ruby/gems/shared/gems/i18n-1.3.0/lib/i18n/backend/simple.rb:77:in `init_translations'
     # /home/w/.rbenv/versions/jruby-9.2.0.0/lib/ruby/gems/shared/gems/i18n-1.3.0/lib/i18n/backend/simple.rb:87:in `lookup'
     # /home/w/.rbenv/versions/jruby-9.2.0.0/lib/ruby/gems/shared/gems/i18n-1.3.0/lib/i18n/backend/base.rb:32:in `translate'
     # /home/w/.rbenv/versions/jruby-9.2.0.0/lib/ruby/gems/shared/gems/i18n-1.3.0/lib/i18n.rb:185:in `block in translate'
     # org/jruby/RubyKernel.java:1180:in `catch'
     # /home/w/.rbenv/versions/jruby-9.2.0.0/lib/ruby/gems/shared/gems/i18n-1.3.0/lib/i18n.rb:181:in `translate'
     # /home/w/.rbenv/versions/jruby-9.2.0.0/lib/ruby/gems/shared/gems/i18n-1.3.0/lib/i18n/backend/transliterator.rb:13:in `transliterate'
     # /home/w/.rbenv/versions/jruby-9.2.0.0/lib/ruby/gems/shared/gems/i18n-1.3.0/lib/i18n.rb:263:in `transliterate'
     # /home/w/.rbenv/versions/jruby-9.2.0.0/lib/ruby/gems/shared/gems/activesupport-5.2.2/lib/active_support/inflector/transliterate.rb:64:in `transliterate'
     # /home/w/.rbenv/versions/jruby-9.2.0.0/lib/ruby/gems/shared/gems/activesupport-5.2.2/lib/active_support/inflector/transliterate.rb:94:in `parameterize'
     # /home/w/.rbenv/versions/jruby-9.2.0.0/lib/ruby/gems/shared/gems/activesupport-5.2.2/lib/active_support/core_ext/string/inflections.rb:191:in `parameterize'
     # org/jruby/RubyKernel.java:1942:in `public_send'
     # /home/w/.rbenv/versions/jruby-9.2.0.0/lib/ruby/gems/shared/gems/activesupport-5.2.2/lib/active_support/core_ext/object/try.rb:19:in `try!'
     # /home/w/.rbenv/versions/jruby-9.2.0.0/lib/ruby/gems/shared/gems/activesupport-5.2.2/lib/active_support/core_ext/object/try.rb:8:in `try'
     # ./spec/app/models/address.rb:6:in `block in ___id_default__'
     # ./lib/mongoid/fields/standard.rb:241:in `evaluate_default_proc'
     # ./lib/mongoid/fields/standard.rb:223:in `evaluated_default'
     # ./lib/mongoid/fields/standard.rb:45:in `eval_default'
     # ./lib/mongoid/fields.rb:104:in `apply_default'
     # ./lib/mongoid/fields.rb:89:in `block in apply_post_processed_defaults'
     # org/jruby/RubyArray.java:1801:in `each'
     # ./lib/mongoid/fields.rb:88:in `apply_post_processed_defaults'
...

@enebo
Copy link
Member

enebo commented Dec 27, 2018

@p-mongo yeah we noticed that last week when we thought we were going to put out 9.2.6.0 (same day they released with the refinements version of i18n). So 9.2.6.0 will for sure get a fix for this, but we are on xmas break until new year. Expect greenness at beginning of January.

@andyatkinson
Copy link

I tried the evil seed gem and we're running Rails 4.2.11 and Jruby 9.2.5.0, and I noticed it's using refinements to back port Rails in_batches functionality to Active Record Relation.

I opened an issue here:
evilmartians/evil-seed#6

I believe based on this thread, that refinements are not supported correctly on this version of JRuby. Is that correct?

@headius
Copy link
Member

headius commented Feb 26, 2019

@andyatkinson Can you check whatever bug you're interested in again once we land #5627 (ideally tomorrow or Wednesday) and see if you still have issues? I suspect this will be fixed, or at least improved, by #5604 and #5627.

@headius
Copy link
Member

headius commented Feb 26, 2019

Oh and if there's still issues...can I humbly request you submit a new issue with a new example script, so we can keep the different issues straight?

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

8 participants