Skip to content

Enumerator acts differently when Symbol#to_proc is used #7580

@godfat

Description

@godfat

Thank you for fixing the bugs I reported so quickly. It's very encouraging to report more.

Environment Information

Provide at least:

  • JRuby version (jruby -v) and command line (flags, JRUBY_OPTS, etc)

    jruby 9.4.0.0 (3.1.0) 2022-11-23 95c0ec159f OpenJDK 64-Bit Server VM 19.0.1+10 on 19.0.1+10 +jit [x86_64-linux]
    
  • Operating system and platform (e.g. uname -a)

    Linux li1544-77 5.13.4-x86_64-linode146 #1 SMP Wed Jul 21 14:45:45 EDT 2021 x86_64 GNU/Linux
    

Expected Behavior

Prints true with this line:

jruby -ve 'p({key: BasicObject.new}.each_value.all?(&:__id__))'

It should be the same as using a block.

jruby -ve 'p({key: BasicObject.new}.each_value.all?{|v|v.__id__})'

Actual Behavior

Block form prints true is expected. With Symbol#to_proc it gives an error:

NoMethodError: undefined method `respond_to?' for #<BasicObject:0x100d0218>
  each_value at org/jruby/RubyHash.java:1630
        each at org/jruby/RubyEnumerator.java:397
        all? at org/jruby/RubyEnumerable.java:1694
      <main> at -e:1
... 5 levels...

Where I hit into this issue, the value was a proxy object which inherits from BasicObject, which has method_missing implemented, therefore it returns true when it's asked with respond_to?, and then it'll be asked with to_ary but then it did not return an array, therefore giving another different error.

The actual code can be found here, where I put a workaround for JRuby: https://github.com/godfat/muack/blob/muack-1.7.0/lib/muack/session.rb#L25-L28

I am not sure why to_ary is needed here, and this might help debug it so I mentioned this.

There are more obscure and weird compatibility issue with JRuby with this library. I'll try to reproduce them with one liners. If you search RUBY_ENGINE == 'jruby' you can find a few of them.

I do recall there's one which Ruby 2.x was giving the same error, but not anymore after Ruby 3.0. I would guess JRuby here continues the behaviour of Ruby 2.x, which is reasonable even though now it should follow the behaviour of 3.0+. Not sure how to reproduce this in one liner yet, but I'll try later.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions