Mega Code Archive

 
Categories / Ruby / Array
 

Each from both sides

class Array  def each_from_both_sides()     front_index = 0     back_index = self.length-1     while front_index <= back_index       yield self[front_index]       front_index += 1       if front_index <= back_index     yield self[back_index]         back_index -= 1       end     end   end end %w{1,2,3,4,5}.each_from_both_sides { |x| puts x }