
module Enumerable

  class <<self
    #
    # Invokes a block once for every element in a sequence of
    # Enumerables.
    #
    def chain *enums
      return enum_for(:chain, *enums) unless block_given?
      enums.each do |enum|
        enum.each {|*args| yield(*args) }
      end
    end
  end

  if RUBY_VERSION >= '2.6'
    warn "warning: Enumerable\#chain defined since Ruby 2.6 is incompatible with this gem when used with args and a block"
    undef chain
  end

  #
  # Creates a chain of Enumerables following this one, and
  # invokes a block once for each element of each Enumerable.
  #
  def chain *enums
    return enum_for(:chain, *enums) unless block_given?
    [self, *enums].each do |enum|
      enum.each {|*args| yield(*args) }
    end
    nil
  end

end

=begin
Copyright (c) 2018, Matthew Kerwin <matthew@kerwin.net.au>

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
=end

