From f1354df6046b6c7f0aa85ee8386b73d1ea1eac64 Mon Sep 17 00:00:00 2001 From: Joel Drapper Date: Mon, 2 Dec 2024 19:15:15 +0000 Subject: [PATCH] Literal::Array#take Closes #207 --- lib/literal/array.rb | 4 ++++ test/array.test.rb | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/lib/literal/array.rb b/lib/literal/array.rb index ac5dbbb..840ba0b 100644 --- a/lib/literal/array.rb +++ b/lib/literal/array.rb @@ -484,6 +484,10 @@ def sort!(...) self end + def take(...) + __with__(@__value__.take(...)) + end + def to_a @__value__.dup end diff --git a/test/array.test.rb b/test/array.test.rb index 144f32a..0692bb1 100644 --- a/test/array.test.rb +++ b/test/array.test.rb @@ -599,3 +599,12 @@ expect(result.to_a) == [2, 3, 1] expect(array.to_a) == [1, 2, 3] end + +test "#take takes the first n elements" do + array = Literal::Array(Integer).new(1, 2, 3, 4, 5) + + result = array.take(2) + assert Literal::Array(Integer) === result + expect(result.to_a) == [1, 2] + expect(array.to_a) == [1, 2, 3, 4, 5] +end