有人可以幫助newb嗎?我遇到了錯誤;沒有方法錯誤,來自不同類的調用方法,傳遞參數
: undefined method `computer_name' for nil:NilClass (NoMethodError)
我想在ruby中調用帶.method_name的方法嗎?
是因爲我的初始化方法不正確?沒有正確傳遞參數?
load 'admin_logic.rb'
load 'computer_logic.rb'
load 'user_logic.rb'
class TicTacToe
def initialize(admin_object, computer_object, user_object)
admin_object = Admin.new
computer_object = ComputerLogic.new
user_object = UserLogic.new
@new_game = TicTacToe.new(admin_object, computer_object, user_object)
end
end
puts "Hello, I\'m " + @new_game.computer_name + ", let\'s play Tic Tac Toe! What is your name?"
puts "Great " + @new_game.user_name + ", you\'ll be " + @new_game.user_sign + ". Please choose where you want to go."
puts 'The game board is the following, please remember!'
puts ' a1 | a2 | a3'
puts " --- --- ---"
puts ' b1 | b2 | b3'
puts " --- --- ---"
puts ' c1 | c2 | c3'
@new_game.user_sign
@new_game.computer_sign
@new_game.game_board
@new_game.winning_propositions
while @new_game .computer_win != true do
@new_game.user_turn
@new_game.draw_game_outcome
@new_game.player_first_turn_check?
@new_game.draw_game_outcome
end
我的課,我稱之爲FROM - ADMIN.RB
class Admin
def computer_name
@computer_name = "Watson"
end
end
我不會看到'需要'管理員行。 – vgoff
或者'需要'ADMIN'',因爲它在呼叫上限。 – vgoff
但是,在完成之後,您需要在其存在的類的實例上調用該方法。 – vgoff