Skip to content

Commit

Permalink
WIP - Addresses joeldrapper#211
Browse files Browse the repository at this point in the history
  • Loading branch information
hslzr committed Nov 14, 2024
1 parent f53c4da commit 4341ca6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/literal/array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,18 @@ def to_a

alias_method :to_ary, :to_a

def transpose
unless @__type__ == Array
raise ArgumentError.new("#transpose expects Array type")
end

if @__type__ == Array
__with__(@__value__.transpose)
elsif @__type__ == Literal::Array
# TODO: Implement transpose for Literal::Array
end
end

def unshift(value)
Literal.check(actual: value, expected: @__type__) do |c|
c.fill_receiver(receiver: self, method: "#unshift")
Expand Down
13 changes: 13 additions & 0 deletions test/array.test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,16 @@

expect { array.replace("not an array") }.to_raise(ArgumentError)
end

test "#transpose raises if type is not Array" do
array = Literal::Array(Integer).new(1, 2, 3)

expect { array.transpose }.to_raise(ArgumentError)
end

test "#transpose returns a new array with rows and columns transposed" do
array = Literal::Array(Array).new([1, 2], [3, 4], [5, 6])

transposed = array.transpose
expect(transposed.to_a) == [[1, 3, 5], [2, 4, 6]]
end

0 comments on commit 4341ca6

Please sign in to comment.