From c301c45be21887b07288b3d91bfeec5c8df052d7 Mon Sep 17 00:00:00 2001 From: amihaiemil Date: Tue, 28 Apr 2020 16:58:42 +0300 Subject: [PATCH] #14 skip() doesn't mutate the instance --- lib/fromarray.rb | 4 ++-- test/fromarray_test.rb | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/fromarray.rb b/lib/fromarray.rb index 81c931f..fab13da 100644 --- a/lib/fromarray.rb +++ b/lib/fromarray.rb @@ -24,6 +24,7 @@ module Stream # A stream implemented based on an Array. + # This class is immutable and thread-safe. # Author:: Mihai Andronache (amihaiemil@gmail.com) class FromArray def initialize(array) @@ -38,8 +39,7 @@ def skip(count) @array.each_with_index do |val, index| skipped.push(val) unless index + 1 <= count end - @array = skipped - self + FromArray.new(skipped) end # Collect the stream's data into an array and return it. diff --git a/test/fromarray_test.rb b/test/fromarray_test.rb index b96cd06..5be1fd4 100644 --- a/test/fromarray_test.rb +++ b/test/fromarray_test.rb @@ -68,8 +68,8 @@ def test_skip_argument_error_count_negative def test_skip_returns_self stream = FromArray.new([1, 2, 3]) assert( - stream.skip(1).equal?(stream), - 'Method skip should return the modified stream' + !stream.skip(1).equal?(stream), + 'Method skip should return a new instance of the modified stream' ) end