Posted By

gusto on 11/03/06


inject enumerable

Versions (?)

Who likes this?

2 people have marked this snippet as a favorite


Use for Enumerable#inject

 / Published in: Ruby

Need a way to count the number of non empty strings in an array?

  1. def non_empty_string_count(array)
  2. array.inject(0) {|count,a| count += (a && a.length > 0 ? 1 : 0)}
  3. end

Report this snippet  

You need to login to post a comment.