Skip to content

Commit

Permalink
Implements #inspect, #to_s (#240)
Browse files Browse the repository at this point in the history
Closes #179 and #210

Co-authored-by: Joel Drapper <joel@drapper.me>
  • Loading branch information
hslzr and joeldrapper authored Nov 28, 2024
1 parent 4f4df01 commit 0194a1e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/literal/array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,10 @@ def insert(index, *value)
self
end

def inspect
@__value__.inspect
end

def intersection(*values)
values.map! do |value|
case value
Expand Down Expand Up @@ -428,6 +432,8 @@ def to_a

alias_method :to_ary, :to_a

alias_method :to_s, :inspect

def uniq
__with__(@__value__.uniq)
end
Expand Down
12 changes: 12 additions & 0 deletions test/array.test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,18 @@
expect { array.fetch(3) }.to_raise(IndexError)
end

test "#inspect returns a string representation of the array" do
array = Literal::Array(Integer).new(1, 2, 3)

expect(array.inspect) == "[1, 2, 3]"
end

test "#to_s returns a string representation of the array" do
array = Literal::Array(Integer).new(1, 2, 3)

expect(array.to_s) == "[1, 2, 3]"
end

test "#fetch returns default value if element is missing at index" do
array = Literal::Array(Integer).new(1, 2, 3)

Expand Down

0 comments on commit 0194a1e

Please sign in to comment.