(1..100).inject(:+)

Tuesday, July 20, 2010

>> (1..100).inject(:+)
=> 5050

>> [1,2,3,4,5,6,7,8,9].inject([]){|arr,item| item % 2 == 0 ? arr << item : arr }
=> [2, 4, 6, 8]

>> [1].inject([]){|arr,item| item % 2 == 0 ? arr << item : arr } => []
>> [1].inject([]){|arr,item| arr << item if item % 2 == 0}
=> nil

This entry was tagged hidden-features, Ruby and tips

comments powered by Disqus

© 2009-2013 lxneng.com. All rights reserved. Powered by Pyramid

go to Top