Skip to content

Implicit blocks from Symbol#to_proc does surprising and wrong things (with --1.9) #274

@bjeanes

Description

@bjeanes

I've had trouble articulating this, so hopefully the following code will make it pretty explanatory:

Code

(Variants also available https://gist.github.com/3519907 and https://gist.github.com/3519564)

class Hash
  def map_values
    map { |k, v| [k, yield(v)] }
  end
end

p data = {"x"=>[{"y"=>42}]}

p(data.map_values { |x| x.first })
p data

p data.map_values(&:first)
p data

Correct output (jruby-1.6.7.2 --1.8, jruby-1.7.0-preview2 --1.8, mri 1.8.7, mri 1.9.2-p290)

{"x"=>[{"y"=>42}]}
[["x", {"y"=>42}]]
{"x"=>[{"y"=>42}]}
[["x", {"y"=>42}]]
{"x"=>[{"y"=>42}]}

Incorrect output (jruby-1.6.7.2 --1.9, jruby-1.7.0-preview2 --1.9)

{"x"=>[{"y"=>42}]}
[["x", {"y"=>42}]]
{"x"=>[{"y"=>42}]}
[["x", ["y", 42]]]
{"x"=>[]}

In my specific case, not only does my method (contrived — simplified from a real world project) very much return the wrong thing, it mutates the original Hash, even though no mutative methods were used.

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