Skip to content

Commit

Permalink
Implements #flatten! method (#238)
Browse files Browse the repository at this point in the history
* Implements #flatten! method

Closes #174

* rubocop
  • Loading branch information
hslzr authored Nov 28, 2024
1 parent 613ba62 commit 1305fb4
Show file tree
Hide file tree
Showing 2 changed files with 16 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 @@ -247,6 +247,10 @@ def first(...)
@__value__.first(...)
end

def flatten!(...)
@__value__.flatten!(...) ? self : nil
end

def freeze
@__value__.freeze
super
Expand Down
12 changes: 12 additions & 0 deletions test/array.test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,18 @@
}.to_raise(ArgumentError)
end

test "#flatten! flattens the array" do
array = Literal::Array(Array).new([1, 2], [3, 4])

expect((array.flatten!).to_a) == [1, 2, 3, 4]
end

test "#flatten! returns nil if no nested arrays" do
array = Literal::Array(Integer).new(1, 2, 3)

expect(array.flatten!) == nil
end

test "#flatten flattens the array" do
array = Literal::Array(Array).new([1, 2], [3, 4])

Expand Down

0 comments on commit 1305fb4

Please sign in to comment.