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