diff --git a/lib/parfait/list.rb b/lib/parfait/list.rb index 9a9bb9ad..95b4b8a9 100644 --- a/lib/parfait/list.rb +++ b/lib/parfait/list.rb @@ -19,11 +19,13 @@ module Parfait internal_object_length - 1 end + # index of item, remeber first item has index 1 + # return nil if no such item def index_of( item ) max = self.get_length counter = 1 while( counter < max ) - if( get(index) == item) + if( get(counter) == item) return counter end counter = counter + 1 @@ -31,6 +33,11 @@ module Parfait return nil end + # include? means non nil index + def include? item + return index_of(item) != nil + end + # push means add to the end # this automatically grows the List def push value diff --git a/test/parfait/test_list.rb b/test/parfait/test_list.rb index 13931d54..cd5bac37 100644 --- a/test/parfait/test_list.rb +++ b/test/parfait/test_list.rb @@ -49,4 +49,14 @@ class TestList < MiniTest::Test assert_equal v , @list.get(k) end end + def test_index_of + test_many_get + assert_equal 2 , @list.index_of( :two ) + assert_equal nil , @list.index_of( :four ) + end + def test_incude + test_many_get + assert_equal true , @list.include?( :two ) + assert_equal false , @list.include?( :four ) + end end