From 137d3c92318654b761ab484b752ba69ed4238361 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Thu, 8 Dec 2016 12:48:39 +0200 Subject: [PATCH] add each_with_index to indexed --- lib/typed/parfait/indexed.rb | 10 ++++++++++ test/typed/parfait/test_list.rb | 6 ++++++ 2 files changed, 16 insertions(+) diff --git a/lib/typed/parfait/indexed.rb b/lib/typed/parfait/indexed.rb index 140b90a0..b9e65b70 100644 --- a/lib/typed/parfait/indexed.rb +++ b/lib/typed/parfait/indexed.rb @@ -112,6 +112,16 @@ module Parfait self end + def each_with_index + index = 1 + while index <= self.get_length + item = get(index) + yield item , index + index = index + 1 + end + self + end + def each_pair index = 1 while index <= self.get_length diff --git a/test/typed/parfait/test_list.rb b/test/typed/parfait/test_list.rb index 52e9392f..dc4b9547 100644 --- a/test/typed/parfait/test_list.rb +++ b/test/typed/parfait/test_list.rb @@ -115,6 +115,12 @@ class TestList < MiniTest::Test end assert_equal 0 , shouldda_values.length end + def test_each_index + set_shouldda + @list.each_with_index do |val , index| + assert_equal @list[index] , val + end + end def test_each_pair_length shouldda_values = set_shouldda.values @list.each_pair do |key,val|