Skip to content

Commit 8272890

Browse files
authoredNov 28, 2024
Implements #join, #keep_if (#243)
* Implements #join, closes #182 * Implements #keep_if, closes #183 * Rubocop
1 parent a68a750 commit 8272890

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed
 

‎lib/literal/array.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,20 @@ def intersection(*values)
295295
__with__(@__value__.intersection(*values))
296296
end
297297

298+
def join(...)
299+
@__value__.join(...)
300+
end
301+
302+
def keep_if(...)
303+
return_value = @__value__.keep_if(...)
304+
case return_value
305+
when Array
306+
self
307+
else
308+
return_value
309+
end
310+
end
311+
298312
def last(...)
299313
@__value__.last(...)
300314
end

‎test/array.test.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,25 @@
354354
expect(intersection.to_a) == [2]
355355
end
356356

357+
test "#join joins the elements into a string" do
358+
array = Literal::Array(Integer).new(1, 2, 3)
359+
360+
expect(array.join(", ")) == "1, 2, 3"
361+
expect(array.join) == "123"
362+
end
363+
364+
test "#keep_if keeps elements that match the block" do
365+
array = Literal::Array(Integer).new(1, 2, 3)
366+
367+
expect((array.keep_if { |i| i > 1 }).to_a) == [2, 3]
368+
end
369+
370+
test "#keep_if returns an enumerator if no block is given" do
371+
array = Literal::Array(Integer).new(1, 2, 3)
372+
373+
expect(array.keep_if.class.name) == "Enumerator"
374+
end
375+
357376
test "#replace replaces with passed in array" do
358377
array = Literal::Array(Integer).new(1, 2, 3)
359378

0 commit comments

Comments
 (0)