Each with Index Method

Hey everyone, I’m super new to ruby, and I am having trouble with a
method that I wrote using each_with_index method.

I created a method so that I can get the index of an element in the
argument array. Not only do I want to know the value of the element that
I’m looping over, but I also want to know the position in the
collection. I wrote a method called add_value_and_index, that takes an
array of numbers and returns a new array (empty set) composed of
value-plus-the-index of each element in the argument array.

Can someone look over and provide feedback please? Thank you.

Please do not post source code as images.

You are defining a method #initialize inside the other method. That
does not make sense at all, especially since #initialize is the private
initializer for new instances.

You are using attr_accessor in the method. That is also something that
must be used in a class or module scope.

Also, your syntax for what seems to be block parameters is wrong. It
must be

…each_with_index {|element, index| …}

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs