🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of angelikatyborska

angelikatyborska's solution

to Accumulate in the Ruby Track

Published at Jul 13 2018 · 1 comment
Test suite


This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Implement the accumulate operation, which, given a collection and an operation to perform on each element of the collection, returns a new collection containing the result of applying that operation to each element of the input collection.

Given the collection of numbers:

  • 1, 2, 3, 4, 5

And the operation:

  • square a number (x => x * x)

Your code should be able to produce the collection of squares:

  • 1, 4, 9, 16, 25

Check out the test suite to see the expected function signature.


Keep your hands off that collect/map/fmap/whatchamacallit functionality provided by your standard library! Solve this one yourself using other basic tools instead.


It is typical to call #to_enum when defining methods for a generic Enumerable, in case no block is passed.

Here is an additional test you could add:

def test_accumulate_when_block_is_deferred
  accumulate_enumerator = [1, 2, 3].accumulate
  accumulated_result = accumulate_enumerator.each do |number|
    number * number
  assert_equal [1, 4, 9], accumulated_result

For installation and learning resources, refer to the exercism help page.

For running the tests provided, you will need the Minitest gem. Open a terminal window and run the following command to install minitest:

gem install minitest

If you would like color output, you can require 'minitest/pride' in the test file, or note the alternative instruction, below, for running the test file.

Run the tests from the exercise directory using the following command:

ruby accumulate_test.rb

To include color from the command line:

ruby -r minitest/pride accumulate_test.rb


Conversation with James Edward Gray II https://twitter.com/jeg2

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.


require 'minitest/autorun'
require_relative 'accumulate'

class ArrayTest < Minitest::Test
  def test_empty_accumulation
    assert_equal [], [].accumulate { |e| e * e }

  def test_accumulate_squares
    result = [1, 2, 3].accumulate do |number|
      number * number
    assert_equal [1, 4, 9], result

  def test_accumulate_upcases
    result = %w(hello world).accumulate(&:upcase)
    assert_equal %w(HELLO WORLD), result

  def test_accumulate_reversed_strings
    result = %w(the quick brown fox etc).accumulate(&:reverse)
    assert_equal %w(eht kciuq nworb xof cte), result

  def test_accumulate_recursively
    result = %w(a b c).accumulate do |char|
      %w(1 2 3).accumulate do |digit|
    assert_equal [%w(a1 a2 a3), %w(b1 b2 b3), %w(c1 c2 c3)], result

  def test_do_not_change_in_place
    original = [1, 2, 3]
    copy = original.dup
    original.accumulate { |n| n * n }
    assert_equal copy, original

  # Problems in exercism evolve over time, as we find better ways to ask
  # questions.
  # The version number refers to the version of the problem you solved,
  # not your solution.
  # Define a constant named VERSION inside of the top level BookKeeping
  # module.
  #  In your file, it will look like this:
  # module BookKeeping
  #   VERSION = 1 # Where the version number matches the one in the test.
  # end
  # If you are curious, read more about constants on RubyDoc:
  # http://ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/constants.html

  def test_bookkeeping
    assert_equal 1, BookKeeping::VERSION
module Enumerable
  def accumulate(&block)
    each_with_object(self.class.new) do |item, collection|
      collection << block.call(item)

Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of angelikatyborska

I just discovered each_with_object.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?