#---
# Excerpted from "Programming Ruby 3.2",
# published by The Pragmatic Bookshelf.
# Copyrights apply to this code. It may not be used to create training material,
# courses, books, articles, and the like. Contact us if you are in doubt.
# We make no guarantees that this code is fit for any purpose.
# Visit https://pragprog.com/titles/ruby6 for more book information.
#---
class RactorExample
  def initialize
    temporary_port = Ractor::Port.new
    @receiver = Ractor.new(temporary_port) do |tp|
      queue = Ractor::Port.new
      tp << queue
      all_data = ""
      while data = queue.receive do
        all_data += "#{data} "
      end
      all_data
    end
    @queue = temporary_port.receive
  end

  def start
    Ractor.new(@queue) do |receiver|
      receiver << "banana"
      receiver << "orange"
      receiver << nil
    end
    puts @receiver.value
  end
end

RactorExample.new.start
