Hello coders, In this post, you will learn how to solve HackerRank Ruby Enumerable each_with_index Solution. This problem is a part of the Ruby Tutorial series. One more thing to add, don’t straight away look for the solutions, first try to solve the problems by yourself. If you find any difficulty after trying several times, then look for the solutions.

HackerRank Ruby Enumerable each_with_index
Let’s get started with HackerRank Ruby Enumerable each_with_index
Problem Statement
In the previous challenge, we learned about each
method being central to all of the methods provided by Enumerable
class. One of such useful methods is each_with_index
which allows you to iterate over items along with an index keeping count of the item.
For example,
> colors = ['red', 'green', 'blue'] > colors.each_with_index { |item, index| p "#{index}:#{item}" } "0:red" "1:green" "2:blue"
As you can note, the counting of items starts from 0.
In this challenge, your task is to complete the skip_animals
method that takes an animals
array and a skip
integer and returns an array of all elements except first skip
number of items as shown in the example below.
For example,
> skip_animals(['leopard', 'bear', 'fox', 'wolf'], 2) => ["2:fox", "3:wolf"]
It is guaranteed that number of items in animals
array is greater than the value of skip
.
HackerRank Ruby Enumerable each_with_index Solution
def skip_animals(animals, skip) arr = [] animals.each_with_index do |item, index| arr << "#{index}:#{item}" unless index < skip end return arr end
Note: This problem (Ruby Enumerable each_with_index) is generated by HackerRank but the solution is provided by Chase2Learn. This tutorial is only for Educational and Learning purpose.