-1

我尝试了以下方法:

require "socket"

class IRC
def initialize(server, port=6667, user='ruby-bot')
    @server = server
    @port = port
    @user = user
end

def connect!
    @s = TCPSocket.open(@server, @port) #connect
    raise "Couldn't connect to #{@server}:#{@port}" unless @s #error handling
    @s.puts "USER #{@user} +B :IRC Ruby Bot" #set modes etc
end 

def nick(nick=nil)
    @s.puts "NICK #{@nick}"
end

def join(channel)
    @s.puts "JOIN #{channel}"
end
end

接着:

#!/usr/bin/ruby
require './irc.rb'

print 'Server: '
server = gets
print 'Port (6667): '
port = gets

if port.match(/^\n/)
port = '6667'
end

bot = IRC.new(server, port)
bot.nick 'fbot1830'
bot.join '#myowntestchannel'

我收到以下错误:

./irc.rb:17:in `nick': private method `puts' called for nil:NilClass (NoMethodError) from ./bot.rb:14

这是我第一次尝试用 ruby​​ 编写自定义类,请耐心等待,我希望我的错误不会太微不足道;)

4

1 回答 1

7

@s在方法中初始化connect!,但从不调用该方法,因此在方法中@s计算为。nilnick

于 2012-11-03T17:14:25.413 回答