【问题标题】:How to fix this error message? syntax error, unexpected tIVAR如何修复此错误消息?语法错误,意外的 tIVAR
【发布时间】:2013-12-16 02:19:22
【问题描述】:

我正在尝试对我的代码进行一些测试,但遇到了问题。对于非常简单的代码行,我收到一条奇怪的错误消息。此测试是为了确保我的服务器可以接收来自我的一个客户的信息。

没有这一行,测试和文件运行良好:

client_1.@socket.puts("This gives an error.")

包含那段代码会出现此错误:

macowner:WAR3 macowner$ ruby ServerTests1.rb
ServerTests1.rb:160: syntax error, unexpected tIVAR, expecting '('
        client_1.@socket.puts("Output for Server to receive.")  #Error
                        ^

非常感谢您的帮助。我经常收到这个错误,但我不知道它是什么意思(即使搜索它)。

enter code here
require 'minitest/autorun'
require 'socket'
require_relative 'WarGame_Class.rb'
require_relative 'ModifiedPlayer_Class.rb'
require_relative 'DeckClass.rb'

class WarServer

    def initialize(host, port)  
        @socket_server = TCPServer.new(host, port)
        @players = [Player.new, Player.new]
        @deck = CardDeck.new
        @deck.deal_cards(@players[0].cards, @players[1].cards)
        game = WarGame.new
        @clients = {} # keys are sockets, values are players

    end

    def client_keys(key)
      @clients.keys[key] # this should work
    end

    def input   #input reader function
        @input
    end


    def close
        @socket_server.close
    end


    def capture_input   ##input client to get what they wrote
        @input = @clients.keys[0].read_nonblock(1000) # arbitrary max number of bytes

    end

    def accept_client
        #Hash here to link client to player? (or game?)
        client = @socket_server.accept
        @clients[client] = @players[@clients.size]
    #   puts "clients key 0: #{@clients.keys[0]}"
        puts
    #   puts "clients values: #{@clients.values}"
        if @clients.size == 2
            start_game#####################!!!! Starts game if two clients  can put client messages in start game
        end
    end


    def start_game  ##############!!!
        @clients.keys[0].puts  "Welcome to War.  Please press enter to play your card"
        @clients.keys[1].puts  "Welcome to War.  Please press enter to play your card"

    end

end

class MockWarClient
    def initialize
        @socket = TCPSocket.new('localhost', 2012)
    end

    def output
        @output
    end

    def input
        @input
    end

    def capture_output  #need to add (socket)?  How else read from specific socket?
        @output = @socket.read_nonblock(1000) # arbitrary max number of bytes
    rescue
        @output = "capture_output error."
    end

    def write_input
        @input = @war_server.client_keys.write_nonblock(1000)
    end
end

class WarServerTest < MiniTest::Unit::TestCase


    def setup   #This would be like our INITIALIZE Function
        #anything is available through out all tests (i.e., instance vars)
        @war_server = WarServer.new('localhost', 2012)
    end


    def teardown
        @war_server.close
    end

def test_server_capture_output_from_client

        client_1 = MockWarClient.new
        @war_server.accept_client

        client_2 = MockWarClient.new
        @war_server.accept_client

        client_1.@socket.puts("Output for Server to receive.")  #Line I need to fix

        #SOCKETforSERVER
        #clien_n.SOCKETforSERVER.puts''
        #Replace puts with write_nonblock, perhaps  
    end
end

【问题讨论】:

    标签: ruby sockets syntax-error iostream


    【解决方案1】:

    如果您尝试访问@socket 实例变量,那么您只需要MockWarClient 中的另一个访问器方法:

    def socket
      @socket
    end
    

    然后说client_1.socket.puts(...) 来使用它。你也可以使用attr_reader 来简化事情:

    class MockWarClient
      attr_reader :socket, :input, :output
    
      def initialize
        @socket = TCPSocket.new('localhost', 2012)
      end
    
      def capture_output  #need to add (socket)?  How else read from specific socket?
        @output = @socket.read_nonblock(1000) # arbitrary max number of bytes
      rescue
        @output = "capture_output error."
      end
    
      def write_input
        @input = @war_server.client_keys.write_nonblock(1000)
      end
    end
    

    这将为您创建三个访问器方法。

    【讨论】:

      【解决方案2】:

      您不能像在client_1.@socket 中那样以通常的方式调用名为@socket 的方法。该方法名称非常不寻常,因为它与实例变量混淆,并且您是否真的有这样的方法是值得怀疑的,但如果有,那么调用这种方法的方法是:

      client_1.send(:@socket)
      

      【讨论】:

      • 我不认为他们在尝试调用名为 @socket 的方法,他们显然是在尝试访问实例变量。
      • 它必须是一种方法,因为您永远不会使用带有任何类型变量的点来发送消息。对于解析器来说,它必须是一个方法。
      • @mu 太短了我是一个非常业余的程序员...aaa并且我想我正在尝试在那里调用变量(因为我正在尝试通过套接字传递信息...)
      • @vgoff:当然假设这个人知道 Ruby,但如果他们擅长 Ruby,他们会尝试以这种方式访问​​另一个对象中的实例变量......
      • 你的评论是善意的戳,@muistooshort。并且有机会让 OP 希望阅读它并从中收集一些信息,也许。是的,我相信 Yalo 可能也想传递一个变量。
      猜你喜欢
      • 1970-01-01
      • 2011-09-14
      • 2018-06-18
      • 2018-03-06
      • 1970-01-01
      • 1970-01-01
      • 2014-02-19
      • 2019-10-23
      • 1970-01-01
      相关资源
      最近更新 更多