lib/hm/algo.rb
# Enumerates through entire collection with "current key/current value" at each point, even
# if elements are deleted in a process of enumeration
Enumerates through entire collection with "current key/current value" at each point, even
if elements are deleted in a process of enumeration
Enumerates through entire collection with " current key / current value " at each point even if elements are deleted in a process of enumeration
def robust_enumerator(collection)
      case collection
      when Hash, Struct
        collection.each_pair.to_a
      when Array
        Enumerator.new do |y|
          cur = collection.size
          until cur.zero?
            pos = collection.size - cur
            y << [pos, collection[pos]]
            cur -= 1
          end
        end
      when ->(c) { c.respond_to?(:each_pair) }
        collection.each_pair.to_a
      else
        fail TypeError, "Can't dig/* in #{collection.class}"
      end
    end
def robust_enumerator ( collection ) case collection when Hash , Struct collection . each_pair . to_a when Array Enumerator . new do | y | cur = collection . size until cur . zero? pos = collection . size - cur y << [ pos , collection [ pos ] ] cur -= 1 end end when -> ( c ) { c . respond_to? ( :each_pair ) } collection . each_pair . to_a else fail TypeError , "Can't dig/* in #{collection.class}" end end
Enumerates through entire collection with "current key/current value" at each point, even
if elements are deleted in a process of enumeration
Enumerates through entire collection with " current key / current value " at each point even if elements are deleted in a process of enumeration
collection