Ruby Quicktips Logo

Ruby Quicktips

Random Ruby and Rails tips.
This blog is dedicated to deliver short, interesting and practical tidbits of the Ruby language and Ruby on Rails framework. Read more...

Your submissions are more than welcome!
Apr 4 ’12

Two Ruby tricks using method chaining and Procs

[1,2,3].each_with_object(1).map(&:+)
# => [2, 3, 4]

# Same outcome, even shorter
[1, 2, 3].map(&1.method(:+))
# => [2, 3, 4]

In his blog post In Ruby, &method passes you!, Andrew Grimm explains how this all works.

Both snippets via Peter Cooper.

11 notes 0 comments

  1. ream88 reblogged this from rubyquicktips
  2. haru012 reblogged this from rubyquicktips
  3. rubyquicktips posted this

Comments

You can use HTML tags for formatting. Wrap code in <code> tags and multiple lines of code in <pre><code> tags.

blog comments powered by Disqus