-
-
Notifications
You must be signed in to change notification settings - Fork 942
Closed
Milestone
Description
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 dataCorrect 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.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels