【问题标题】:How can I use method split on ruby module?如何在 ruby​​ 模块上使用方法拆分?
【发布时间】:2021-05-15 03:42:45
【问题描述】:

大家好,尝试在模块中使用拆分方法但我不能,终端给我这个错误:undefined method `split' for :"set algo":Symbol (NoMethodError)

class Server
    def initialize(port,ip)
      @server = TCPServer.open(ip,port)
      @connections = Hash.new
      @clients = Hash.new
      @connections[:server] = @server
      @connections[:clients] = @clients
      run
      split
    end

  def run
    loop{
      Thread.start(@server.accept) do | client |
        # for each user connected and accepted by server, it will create a new thread object
        # and which pass the connected client as an instance to the block
        request = client.gets.chomp.to_sym
        dev =  request.split(" ")
        client.puts dev[0]
         #if dev[0] ==  "set"
          #  client.puts "OPERACION VALIDA"
         #else
          #  client.puts "OPERACION INVALIDA "
          #  Thread.kill self
        #end
      end
    }.join
  end



end
 Server.new(3000, "localhost")

【问题讨论】:

  • 顺便说一句,initialize 的最后一行有一个流浪的split
  • @anonimo342 : 您希望将 split 应用于 symbol 达到什么效果?也许你打算像request.to_s.split(//)这样的东西?

标签: ruby macos server split memcached


【解决方案1】:

替换:

request = client.gets.chomp.to_sym

与:

request = client.gets.chomp

调用to_sym 会将字符串从gets/chomp 转换为不实现split 的符号。

【讨论】:

  • 谢谢!!你的回答解决了我所有的问题
猜你喜欢
  • 2015-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-12
相关资源
最近更新 更多