This is really good! Especially the fact that you're testing it.
But yes, there is a way to make it more idiomatic. For starters, we don't have to maintain the index separately. We can use Enumable#each_with_index
def remove_every_other(list)
results = []
list.each_with_index do |item, i|
results << item if i.even?
end
results
end
Second, we don't need to explicitly return the results array if we use Enumerable#each_with_object.
def remove_every_other(list)
list.each_with_index
.each_with_object([]) do |(item, i), results|
results << item if i.even?
end
end
But even this is a bit long winded. We just need to filter out entries where the
def remove_every_other(list)
list.each_with_index
.filter do |item, i|
i.even?
end.map(&:first)
end
But if we use the suggested Enumerable#each_slice this becomes even more terse because we don't need to map out the index. There's no conditional here.
def remove_every_other(list)
list.each_slice(2).map(&:first)
end
Naming
You're using Ruby arrays. This makes the generic list name a bit unidiomatic. More idiomatic would be something that reflects that they are arrays like arr.
The word "remove" in the method name seems less applicable that "select".
Testing
When you call puts value.inspect, this can simply be p value.
And you're repeating yourself. Better to have an array of test arrays and iterate over them.