We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
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.
doesn't exactly explain what the skip_animals method is supposed to do—you have to return an array of the elements with the indices prefixed like the example shows.
I've noticed a lot of the Ruby challenges hide parts of the source code from you, and I think it can be useful to see everything that's being run to help learn even more about the language. Luckily, with File.read(__FILE__) we can see the full source code.
The full source code for this challenge is as follows.
defskip_animals(animals,skip)# Your code hereenda1=['bat','cow','jaguar','panda','tiger','deer']a2=['leopard','bear','fox','wolf','dog','cat']def_print_animals(animals,skip)animals_=[]animals.each_with_indexdo|item,index|nextifindex<skipanimals_.push("#{index}:#{item}")endanimals_enduser_result1=skip_animals(a1,3)user_result2=skip_animals(a2,0)test_result1=_print_animals(a1,3)test_result2=_print_animals(a2,0)ifnot(user_result1.is_a?Arrayanduser_result2.is_a?Array)puts'Your method must return an array.'exit(0)endif(user_result1!=test_result1)or(user_result2!=test_result2)puts'Wrong! Please check the return value of your program.'exit(0)endputs'Correct! You have a good understanding of Ruby.'
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Ruby - Enumerable - each_with_index
You are viewing a single comment's thread. Return to all comments →
The challenge statement, here:
doesn't exactly explain what the
skip_animals
method is supposed to do—you have to return an array of the elements with the indices prefixed like the example shows.I've noticed a lot of the Ruby challenges hide parts of the source code from you, and I think it can be useful to see everything that's being run to help learn even more about the language. Luckily, with
File.read(__FILE__)
we can see the full source code.The full source code for this challenge is as follows.