[Code] Ruby snippets

[Code] Ruby snippets

Postby thomthom » Wed Nov 09, 2011 1:32 pm

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 }
=> -5

max = a.inject(0) { |m,i| ( i > m ) ? i : m }
=> 9

total = a.inject(0) { |m,i| i + m }
=> 47
0
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom 
PluginStore Author
PluginStore Author
 

SketchUcation One-Liner Adverts

by Ad Machine » 5 minutes ago



Ad Machine 
Robot
 


 

Return to Developers' Forum

Who is online

Users browsing this forum: No registered users and 15 guests

Visit our sponsors: