Skip to content

Data#to_h passes one Array to the block instead of the key and the value #10066

Closed

Description

@taxor03

With a block, Data#to_h yields one Array [key, value] to the block, not two values. A block with two parameters, { |key, value| }, still works because a proc spreads the Array, and so do { |*args| }, &method(...) with a method that takes two arguments, and a lambda that takes two arguments. A block with one parameter gets the wrong argument: { |key| }, { it } and { _1 } get the whole Array instead of the key, and a lambda that takes one argument is called with the Array instead of raising ArgumentError. The docs show to_h {|name, value| ... } -> hash (Data#to_h), and CRuby yields two values. Struct#to_h with a block already behaves like CRuby on JRuby.

Point = Data.define(:x, :y)
point = Point.new(x: 1, y: 2)

p point.to_h { |key| [key.to_s, 0] }
p point.to_h { [it.to_s, 0] }
p point.to_h { |key, value| [key.to_s, value] }
p((point.to_h(&->(key) { [key.to_s, 0] }) rescue $!))

PointStruct = Struct.new(:x, :y)
p PointStruct.new(1, 2).to_h { |key| [key.to_s, 0] }

CRuby 4.0.9 (ruby 4.0.9 (2026-10-23 revision 3e93b23afc) +PRISM [aarch64-linux]), and 3.4.12 prints the same:

{"x" => 0, "y" => 0}
{"x" => 0, "y" => 0}
{"x" => 1, "y" => 2}
#<ArgumentError: wrong number of arguments (given 2, expected 1)>
{"x" => 0, "y" => 0}

JRuby 10.0.8.0 (jruby 10.0.8.0 (3.4.12) 2026-11-05 ef393d4e54 OpenJDK 64-Bit Server VM 21.0.13+9-LTS on 21.0.13+9-LTS +indy +jit [aarch64-linux]):

{"[:x, 1]" => 0, "[:y, 2]" => 0}
{"[:x, 1]" => 0, "[:y, 2]" => 0}
{"x" => 1, "y" => 2}
{"[:x, 1]" => 0, "[:y, 2]" => 0}
{"x" => 0, "y" => 0}

JRuby 10.1.3.0 (jruby 10.1.3.0 (4.0.9) 2026-11-05 3db52d02f9 OpenJDK 64-Bit Server VM 21.0.13+9-LTS on 21.0.13+9-LTS +indy +jit [aarch64-linux]) and a build of master (54dd973) print the same.

RubyData.to_h yields with block.yieldSpecific(context, k, v) (RubyData.java#L190; on master RubyData.java#L199), which passes the two values as one Array to a block with a single parameter. CRuby's Data#to_h is rb_struct_to_h (struct.c#L2047), which calls rb_yield_values(2, k, v) (struct.c#L1096). RubyStruct#to_h already uses block.yieldValues, and the same call in RubyData.to_h should match CRuby.

ruby/spec's "passes to a block each pair's key and value as separate arguments" checks only { |k, v| } and { |*args| }, which get the same values either way, so it passes on JRuby (to_h_spec.rb#L16-L25).

Found while comparing JRuby with CRuby on ruby/spec-based checks.

Activity

  1. added this to the JRuby 10.0.9.0 milestone on Nov 17, 2026
  2. headius commented on Nov 17, 2026

    @headius
    Member

    Fixed by #10070.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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