Use for Enumerable#inject


/ Published in: Ruby
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.