The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Enumerable#injecting

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Chad Fowler

Posts: 408
Nickname: chadfowler
Registered: Apr, 2003

A programmer, musician, and language addict.
Enumerable#injecting Posted: Aug 2, 2007 11:04 PM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Chad Fowler.
Original Post: Enumerable#injecting
Feed Title: ChadFowler.com
Feed URL: http://feeds2.feedburner.com/Chadfowlercom
Feed Description: Best practices, worst practices, and some generally obvious stuff about programming.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Chad Fowler
Latest Posts From ChadFowler.com

Advertisement

I saw this while browsing the Merb source code, and despite the bad variable names thought it was pretty nice:

module Enumerable
  def injecting(s)
    inject(s) do |k, i|
      yield(k, i); k
    end
  end
end

Use like:

['o', 'hai', 'rly', 'srsly'].injecting({}) {|accumulator, value| accumulator[value] = value.size} 
            # => {"srsly"=>5, "o"=>1, "hai"=>3, "rly"=>3}# => 10

Simple pleasures. I find it increasingly hard to live without things like this, Object#returning, and Symbol#to_proc as I start to use them. After all…

Read: Enumerable#injecting

Topic: flog version 1.0.2 has been released! Previous Topic   Next Topic Topic: Vacation!

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use