![](/img/trans.png)
[英]private method `select' called for nil:NilClass (NoMethodError)
[英]private method `puts' called for nil:NilClass (NoMethodError)
我不確定為什么嘗試從類中的另一種方法訪問實例變量時無法訪問該實例變量的方法。
該程序是一個聊天程序(正在進行中)。 抱歉,如果這是一個菜鳥問題,我正試圖從其他語言中我已經知道的知識中蠻力學習Ruby。
require 'socket' # Sockets are in standard library
class Client
def initilize()
hostname = 'localhost'
port = 2000
@s = TCPSocket.open(hostname, port)
end
def startChat
puts "Starting Client"
message = gets.chomp
@s.puts(message)
while line = @s.gets # Read lines from the socket
puts line.chop # And print with platform line terminator
@s.close # Close the socket when done
gets.chomp
end
end
end
c = Client.new()
c.startChat
def initialize()
有一個錯字,因此在初始化Client實例時不會調用它。 因此, @s
永遠不會被定義(因此為nil)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.