Stop Being Carbon
Subscribe to feed
‹ Presenting Folaroids • Internship ›
January 11, 2008 in Coding | 2 comments
def sum(enum, &block) sum = 0 enum.each do |value| v = block.call(value) sum = v ? v + sum : sum end sum end
Tags: code, ruby
Comments feed for this article
Trackback link
http://www.stopbeingcarbon.com/2008/01/ruby-sum/trackback/
June 10, 2008 at 21:14
oneup
enum.inject(0) {|sum, value| sum += value } ?
June 11, 2008 at 20:18
Christoph
Yep, that’s definitely shorter. But I guess I had a reason do go the complicated way… Had I?
Notify me of followup comments via e-mail
2 comments
Comments feed for this article
Trackback link
http://www.stopbeingcarbon.com/2008/01/ruby-sum/trackback/
June 10, 2008 at 21:14
oneup
enum.inject(0) {|sum, value| sum += value } ?
June 11, 2008 at 20:18
Christoph
Yep, that’s definitely shorter. But I guess I had a reason do go the complicated way… Had I?