When I first started learning Ruby, I was suspicious of its many built in methods. In particular, Ruby’s enumerable methods that both iterate and do something to each element.

Here is a peek behind what is actually going on when you call these enumerable methods on your data structure.

each

  def my_each
    self.length.times do |i|
    yield(self[i])
  end

map

  def my_map
    new_array=[]
    my_each do |elem|
      new_array.push yield elem
    end
    new_array
  end

select

  def my_select
    new_array = []
    self.my_each do |elem|
      if yield elem
        new_array << elem
      end
    end
    new_array
  end

reduce

  def my_reduce(accumulator, starting_value = nil)
    self.my_each do |elem|
      accumulator = yield accumulator, elem
    end
    accumulator
  end