【问题标题】:Why is my class variable still nil after I assign it? (Ruby)为什么我的类变量在我分配后仍然为零? (红宝石)
【发布时间】:2023-03-28 12:53:01
【问题描述】:

根据我的测试,client_1 应该是一个客户端类,我用它来看看能不能输出和放。我需要在我的客户端类中使用 @output 变量仍然为零,但我在我的客户端类中使用 capture_output 方法分配它。

@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

【问题讨论】:

  • 您将client_1 局部变量与@client_1 实例变量混淆了,后者在任何地方都没有定义,因此它等于nil

标签: ruby class sockets variable-assignment iostream


【解决方案1】:

client_1 是一个局部变量,与@client_1 不同。我在这段代码中看不到您分配@client_1 的任何地方。除非有一些您未共享的代码执行此操作,否则 @client_1 将评估为 nil

【讨论】:

  • 谢谢。你们都回答了这个问题。 @client_1 甚至不存在>.>
【解决方案2】:

@client_1 是一个实例变量,它是类实例上的一个变量,在对象存在期间一直存在。为了给实例变量赋值,您必须通过在变量名前附加@来表示它。

相比之下,client_1 是一个局部变量,它存在于单个方法或块中。在您的 sn-p 中,您已将值分配给 client_1 local 变量,但未分配给 @client_1 instance 变量,在分配之前为 nil .

【讨论】:

  • 不敢相信我错过了!感谢您直言不讳。
猜你喜欢
  • 1970-01-01
  • 2012-09-03
  • 2015-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-01
  • 2010-09-28
  • 1970-01-01
相关资源
最近更新 更多