Permalink
Browse files

Implement #rindex

  • Loading branch information...
scooter-dangle committed Apr 25, 2012
1 parent d3cf71f commit 0cebe585f944d882a731f89bd62a884aa1bc9415
Showing with 27 additions and 2 deletions.
  1. +27 −2 lib/iterable.rb
View
@@ -20,15 +20,15 @@ class IterableArray
@@plain_modifiers = [ :delete, :delete_at, :pop ]
@@special_modifiers = [ :clear, :compact!, :insert, :shift, :shuffle!, :sort!, :unshift, :reverse!, :rotate!, :slice!, :swap!, :swap_indices!, :uniq! ]
- @@iterators = [ :delete_if, :each, :reverse_each, :collect, :collect!, :map, :map!, :combination, :cycle, :delete_if, :drop_while, :each_index, :index, :keep_if, :each_with_index, :reject!, :reject, :select!, :select, :take_while, :count ]
+ @@iterators = [ :delete_if, :each, :reverse_each, :rindex, :collect, :collect!, :map, :map!, :combination, :cycle, :delete_if, :drop_while, :each_index, :index, :keep_if, :each_with_index, :reject!, :reject, :select!, :select, :take_while, :count ]
# @@hybrids contains methods that fit into the previous groups depending
# on the arguments passed. (Or depending on how dumb I am)
@@hybrids = [ :fill ]
# The following two lines are supposed to help me keep track of progress.
- # working: Array#instance_methods(false) => [:find_index, :rindex, :sort_by!, :zip, :transpose, :replace, :flatten, :flatten!, :permutation, :repeated_permutation, :repeated_combination, :product, :pack]
+ # working: Array#instance_methods(false) => [:find_index, :sort_by!, :zip, :transpose, :replace, :flatten, :flatten!, :permutation, :repeated_permutation, :repeated_combination, :product, :pack]
# original: Array#instance_methods(false) => [:inspect, :to_s, :to_a, :to_ary, :frozen?, :==, :eql?, :hash, :[], :[]=, :at, :fetch, :first, :last, :concat, :<<, :push, :pop, :shift, :unshift, :insert, :each, :each_index, :reverse_each, :length, :size, :empty?, :find_index, :index, :rindex, :join, :reverse, :reverse!, :rotate, :rotate!, :sort, :sort!, :sort_by!, :collect, :collect!, :map, :map!, :select, :select!, :keep_if, :values_at, :delete, :delete_at, :delete_if, :reject, :reject!, :zip, :transpose, :replace, :clear, :fill, :include?, :<=>, :slice, :slice!, :assoc, :rassoc, :+, :*, :-, :&, :|, :uniq, :uniq!, :compact, :compact!, :flatten, :flatten!, :count, :shuffle!, :shuffle, :sample, :cycle, :permutation, :combination, :repeated_permutation, :repeated_combination, :product, :take, :take_while, :drop, :drop_while, :pack]
def_delegators :@array, *@@plain_accessors
@@ -115,6 +115,7 @@ def last n = nil
# What should I use for the default argument here?
# (What if someone really wants to push `nil` onto the array?)
+ # See also: #rindex
def push obj = nil
@array.push obj unless obj == nil
self
@@ -652,6 +653,30 @@ def reject
out
end
+ # untested
+ # need to fix default argument to allow folks to search for nil
+ def rindex obj = nil
+ if obj.nil?
+ return @array.to_enum(:rindex) unless block_given?
+
+ catch_a_break do
+ toggle_tracking
+ @current_index = @array.size - 1
+ @backward_index, @forward_index = @current_index - 1, @current_index + 1
+ while @current_index >= 0
+ return @current_index if yield @array.at @current_index
+
+ @current_index = @backward_index
+ @forward_index = @current_index + 1
+ @backward_index = @current_index - 1
+ end
+ nil
+ end
+ else
+ @array.rindex obj
+ end
+ end
+
def reverse_each
return @array.to_enum(:reverse_each) unless block_given?

0 comments on commit 0cebe58

Please sign in to comment.