You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: message_chains.rb
    path of file: ./repos/rspec-mocks/lib/rspec/mocks/any_instance
    
    the code of the file until where you have to start completion: module RSpec
  module Mocks
    module AnyInstance
      # @private
      class MessageChains
        def initialize
          @chains_by_method_name = Hash.new { |h, k| h[k] = [] }
        end

        # @private
        def [](method_name)
          @chains_by_method_name[method_name]
        end

        # @private
        def add(method_name, chain)
          @chains_by_method_name[method_name] << chain
          chain
        end

        # @private
        def remove_stub_chains_for!(method_name)
          @chains_by_method_name[method_name].reject! do |chain|
            StubChain === chain
          end
        end

        # @private
        def has_expectation?(method_name)

    
        def unfulfilled_expectations
          @chains_by_method_name.map do |method_name, chains|
            method_name.to_s if ExpectationChain === chains.last && !chains.last.expectation_fulfilled?
          end.compact
        end

        # @private
        def received_expected_message!(method_name)
          @chains_by_method_name[method_name].each do |chain|
            chain.expectation_fulfilled!
          end
        end

        # @private
        def playback!(instance, method_name)
          raise_if_second_instance_to_receive_message(instance)
          @chains_by_method_name[method_name].each do |chain|
            chain.playback!(instance)
          end
        end

      private

        def raise_if_second_instance_to_receive_message(instance)
          @instance_with_expectation ||= instance if ExpectationChain === instance
          return unless ExpectationChain === instance
          return if @instance_with_expectation.equal?(instance)

          AnyInstance.error_generator.raise_second_instance_received_message_error(unfulfilled_expectations)
        end
      end