Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 未通过套接字调用类函数_Ruby - Fatal编程技术网

Ruby 未通过套接字调用类函数

Ruby 未通过套接字调用类函数,ruby,Ruby,我正在尝试编写一个服务器 require 'socket' server = TCPServer.open(2000) loop { Thread.start(server.accept) do |client| client.puts(Time.now.ctime) class DisplayMsg def self.msg(msg) client.puts msg client.close end end

我正在尝试编写一个服务器

require 'socket'

server = TCPServer.open(2000)
loop {
  Thread.start(server.accept) do |client|
    client.puts(Time.now.ctime)

    class DisplayMsg
      def self.msg(msg)
        client.puts msg
        client.close
      end
    end

    client.puts "Select 1,2,3"
    while user_input = client.gets.chomp
      case user_input
      when "1"
        client.puts "Hello"
      when "2"
        client.puts "Alo"
      when "3"
        DisplayMsg.msg("Exiting")
      end
    end
  end
}
当我试图通过调用类中定义的函数来退出时,它就起作用了。在那里,程序挂起并等待

我猜该函数没有被调用,因为当我用
client.put
交换对该函数的调用时,它工作了


非常感谢任何帮助/指针。

启动子线程时,主线程不会等待子线程结束,并且会在子线程完成其工作之前完成,除非您加入子线程