2013-12-15 67 views
0

根據我的測試,client_1應該是一個客戶端類,我已經用它來查看是否可以輸出並放置它。我需要在我的客戶端類中使用@output變量爲STILL NIL,但我將其與我的客戶端類中的capture_output方法一起分配。爲什麼我的類變量在我賦值之後仍然爲零? (Ruby)

@war_server.client_keys(0).puts("Hello Frodo") #works 
temp_holder = client_1.capture_output    #store output from server to temp holder 
puts "Here is the captured input from client_1! : #{temp_holder}"#works 

puts @client_1.output       #DOES NOT WORK. But it should because I assin it in my class and use a reader definition 

這是我的課程和測試的代碼。謝謝!

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 

     #can output @war_server.client_keys, though, if I take out the argument to pass in. 
     #puts "Test_Server_output @client keys #{@war_server.client_keys(player)}" #cient_1? 
     puts "Test_Server_output @client keys 0 #{@war_server.client_keys(0)}" 
     puts "Test_Server_output @client keys 1 #{@war_server.client_keys(1)}" 

     @war_server.client_keys(0).puts("Hello Frodo") 
     temp_holder = client_1.capture_output 
     puts "Here is the captured input from client_1! : #{temp_holder}" 

     #puts @war_server.input 
     puts @client_1.output 
    end 
end 
+0

您將'client_1'局部變量與'@ client_1'實例變量相混淆,該實例變量無處被定義,所以它等於'nil'。 –

回答

0

@client_1是一個實例變量,它是一個類的實例上的變量,並存在該對象存在的持續時間。爲了給一個實例變量賦值,你需要@來表示變量名。

相反,client_1局部變量,它存在僅單一方法或塊內。在你的代碼片段中,你已經爲變量賦值了一個變量,但是沒有賦值變量,這個變量是nil直到賦值。

+0

不能相信我錯過了!謝謝你讓我挺直。 – Yallo

1

client_1是一個局部變量,與@client_1截然不同。在代碼中,我沒有看到您指定@client_1的任何地方。除非有一些代碼不是你分享的,那麼@client_1將評估爲nil

+0

謝謝。你們都回答了這個問題。 @ client_1甚至不存在>。> – Yallo

相關問題