[Code] Ruby snippets
-
Just making a thread to post small little snippets I find useful.
Enumerator#inject
method is one method I've discovered very late, but I'm finding it very useful:` a=[-5,2,5,1,4,8,9,6,2,5,5,1,4]
=> [-5, 2, 5, 1, 4, 8, 9, 6, 2, 5, 5, 1, 4]min = a.inject(0) { |m,i| ( i < m ) ? i : m }
=> -5max = a.inject(0) { |m,i| ( i > m ) ? i : m }
=> 9total = a.inject(0) { |m,i| i + m }
=> 47`
Advertisement