lib/motion-map/map.rb
# iterate over arguments in pairs smartly.
#
iterate over arguments in pairs smartly.
iterate over arguments in pairs smartly .
def each_pair(*args, &block)
        size = args.size
        parity = size % 2 == 0 ? :even : :odd
        first = args.first
        if block.nil?
          result = []
          block = lambda{|*kv| result.push(kv)}
        else
          result = args
        end
        return args if size == 0
        if size == 1
          if first.respond_to?(:each_pair)
            first.each_pair do |key, val|
              block.call(key, val)
            end
            return args
          end
          if first.respond_to?(:each_slice)
            first.each_slice(2) do |key, val|
              block.call(key, val)
            end
            return args
          end
          raise(ArgumentError, 'odd number of arguments for Map')
        end
        array_of_pairs = args.all?{|a| a.is_a?(Array) and a.size == 2}
        if array_of_pairs
          args.each do |pair|
            key, val, *ignored = pair
            block.call(key, val)
          end
        else
          0.step(args.size - 1, 2) do |a|
            key = args[a]
            val = args[a + 1]
            block.call(key, val)
          end
        end
        args
      end
def each_pair ( * args , & block ) size = args . size parity = size % 2 == 0 ? :even : :odd first = args . first if block . nil? result = [ ] block = lambda { | * kv | result . push ( kv ) } else result = args end return args if size == 0 if size == 1 if first . respond_to? ( :each_pair ) first . each_pair do | key , val | block . call ( key , val ) end return args end if first . respond_to? ( :each_slice ) first . each_slice ( 2 ) do | key , val | block . call ( key , val ) end return args end raise ( ArgumentError , 'odd number of arguments for Map' ) end array_of_pairs = args . all? { | a | a . is_a? ( Array ) and a . size == 2 } if array_of_pairs args . each do | pair | key , val , * ignored = pair block . call ( key , val ) end else 0 . step ( args . size - 1 , 2 ) do | a | key = args [ a ] val = args [ a + 1 ] block . call ( key , val ) end end args end
iterate over arguments in pairs smartly.
iterate over arguments in pairs smartly .
args
<not_specific>
block
<not_specific>