public Method

Enumerable.injecting(s) { |k, i| ... }

Abstract the common pattern of injecting a hash into a block to accumulate and return the injected hash.

Both of these are equivalent

[1,2,3].inject({}){|m,i| m[i] = i; m }
[1,2,3].injecting({}){|m,i| m[i] = i }
=>  {1=>1, 2=>2, 3=>3}

The main difference is with injecting you do not have to end the block with ;m to return the accumulated hash m. In this sense it is very much like Object#returning

Source Code

# File merb/core_ext/enumerable.rb, line 14
def injecting(s)
  inject(s) do |k, i|
    yield(k, i); k
  end
end
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.