Created
February 9, 2012 05:39
-
-
Save ProfAvery/1777634 to your computer and use it in GitHub Desktop.
15 ways to sum an array in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'test/unit' | |
class TestSums < Test::Unit::TestCase | |
def setup | |
@a = [2, 5, 18, 27] | |
@sum = 0 | |
end | |
def teardown | |
assert_equal 52, @sum | |
end | |
def test1 | |
i = 0 | |
while i < @a.length | |
@sum += @a[i] | |
i = i + 1 | |
end | |
end | |
def test2 | |
i = 0 | |
begin | |
@sum += @a[i] | |
i = i + 1 | |
end while i < @a.length | |
end | |
def test3 | |
i = 0 | |
until i >= @a.length | |
@sum += @a[i] | |
i = i + 1 | |
end | |
end | |
def test4 | |
i = 0 | |
begin | |
@sum += @a[i] | |
i = i + 1 | |
end until i >= @a.length | |
end | |
def test5 | |
i = 0 | |
loop do | |
@sum += @a[i] | |
i = i + 1 | |
break if i >= @a.length | |
end | |
end | |
def test6 | |
i = 0 | |
@a.length.times { | |
@sum += @a[i] | |
i = i + 1 | |
} | |
end | |
def test7 | |
for i in 0 .. @a.length - 1 | |
@sum += @a[i] | |
end | |
end | |
def test8 | |
for i in @a | |
@sum += i | |
end | |
end | |
def test9 | |
i = @a.to_enum | |
loop do | |
@sum += i.next | |
end | |
end | |
def test10 | |
d = @a.dup | |
loop do | |
i = d.shift | |
break if i.nil? | |
@sum += i | |
end | |
end | |
def test11 | |
0.upto(@a.length - 1){ |i| @sum += @a[i] } | |
end | |
def test12 | |
@a.each { |i| @sum += i } | |
end | |
def test13 | |
@a.each_index { |i| @sum += @a[i] } | |
end | |
def test14 | |
@sum = @a.inject :+ | |
end | |
def test15 | |
@sum = eval @a.join '+' | |
end | |
end |
@sum = @a.reduce(&:+)
@sum = @a.reduce(:+) # With base
@sum = @a.reduce(0, :+) # Without base
@sum = @a.sum # Ruby 2.4+
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@sum = @a.reduce(0) { |x, y| x + y }