Skip to content

Commit

Permalink
Literal::Array#take
Browse files Browse the repository at this point in the history
Closes #207
  • Loading branch information
joeldrapper committed Dec 2, 2024
1 parent 11f94d1 commit f1354df
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/literal/array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,10 @@ def sort!(...)
self
end

def take(...)
__with__(@__value__.take(...))
end

def to_a
@__value__.dup
end
Expand Down
9 changes: 9 additions & 0 deletions test/array.test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit f1354df

Please sign in to comment.