Use inject for nicer output in IRB

Posted: 03/29/2009

Sometimes you want to inspect an array in irb by iterating through it and printing out some key values, for example:

  events.each do |event|
    puts event.title
  end

But if the array is large, you will first have to scroll up through the returned array to get to your desired printout. We can prevent that by using inject instead:

  events.inject do |x, event|
    puts event.title
  end

Now nil will be returned instead of the events array, and your desired output is in view.

About Me

I'm a skier, web developer, entrepreneur, freelancer, and all around stand-up guy living in Manhattan. This is me, repping one of my favorite shirts...

Follow me on twitter or send me an email.

All Posts