Thank you for your response

On Mon, Apr 20, 2009 at 2:13 PM, Yukihiro Matsumoto <matz / ruby-lang.org> wrote:
> For that reason, a method for assoc_to_hash operation might be more
> appropriate.  ¨Βυτ στιμθαφε δουβτ®

Let me attempt to dissolve (some of) your doubts. I checked rapidly
rails' code. I found over 40 "inject({})" and looking at them reveals
that most could be improved using #to_hash.

For example, the first ones I found could be rewritten as follows:

# part_container.rb
attrs = attrs.inject({}) { |h,s| k,v = s.split(/=/, 2); h[k] = v; h3D=>
attrs = attrs.map{ |s| s.split(/=/, 2) }.to_hash


# route_set.rb (meta programming)
args = args.zip(#{route.segment_keys.inspect}).inject({}) do |h, (v, k)|
  h[k] = v
  h
end
# ==>
args = args.zip(#{route.segment_keys.inspect}).to_hash


# route_set.rb
recall.inject({}) do |expiry, (key, recalled_value)|
  expiry[key] = (options.key?(key) && options[key].to_param !=
recalled_value.to_param)
  expiry
end
# ==>
recall.to_hash do |key, recalled_value|
  [key, (options.key?(key) && options[key].to_param != recalled_value.to_param)]
end

# etc...

These are easy to find but are not the only instances that could be
improved, of course. Here's another example I found:

# routes.rb
@parameter_shell ||= returning({}) do |shell|
  requirements.each do |key, requirement|
    shell[key] = requirement unless requirement.is_a? Regexp
  end
end
# ==>
@parameter_shell ||= requirements.reject{|key, requirement|
requirement.is_a? Regexp}.to_hash

I believe the latter form is not only more concise but much more expressive.

My final comment would be that although 40 inject({}), a couple of
returning({}) and other places I don't know of in the whole of rails'
code is not that much. Just for comparision's sake, there are only 2
uses of #assoc, 0 of #rassoc, 1 of #partition, 3 of #grep, etc... So I
believe that #to_hash would be reasonably useful.

Thank you,

Marc-Andr