我想了解爲什麼我得到這個錯誤,我懷疑這是因爲我有我的Controller類和在兩個獨立的Ruby文件中查看類。我被告知使用require_relative'filename'應該將所有代碼從一個文件引用到另一個文件中,但我似乎錯過了一些東西。好吧這裏去,不明白爲什麼我得到一個`初始化':未初始化的常量Controller :: View(NameError)
在controller.rb文件,我有
require_relative 'view'
require_relative 'deck_model'
require_relative 'flashcard_model'
class Controller
def initialize
@deckofcards = Deck.new
@welcome = View.new.welcome
@player_guess = View.new.get_user_guess
@success_view = View.new.success
@failure_view = View.new.failure
end
def run
#Logic to run the game
# @current_card
# @user_guess
puts "Let's see if this prints"
# pull_card_from_deck
end
end
在我view.rb文件,我有,
require_relative 'controller'
class View
attr_accessor :userguess
def initialize (userguess = " ")
@userguess = userguess
end
def welcome
system ("clear")
puts "Welcome! Let's play a game."
puts "I'll give you a definition and you have to give me the term"
puts "Ready..."
end
def get_user_guess
@userguess = gets.chomp.downcase
end
def success
puts "Excellent! You got it."
end
def failure
puts "No, that's not quite right."
end
end
然而,當我運行控制器.rb,出現以下錯誤,
/Users/sean/Projects/flash/source/controller.rb:11:in `initialize': uninitialized constant Controller::View (NameError)
from /Users/sean/Projects/flash/source/controller.rb:51:in `new'
from /Users/sean/Projects/flash/source/controller.rb:51:in `<top (required)>'
from /Users/sean/Projects/flash/source/view.rb:1:in `require_relative'
from /Users/sean/Projects/flash/source/view.rb:1:in `<top (required)>'
from controller.rb:1:in `require_relative'
from controller.rb:1:in `<main>'
任何人都可以請幫我解決這個問題。
你發佈的回溯說錯誤發生在'/Users/sean/Projects/flash/source/controller.rb:51'的第51行,但是你發佈的文件要短得多。請發佈您的整個代碼以及您用來運行它的確切命令。 – 2015-03-02 17:11:37
+1給@DavidGrayson。因爲你的'View'類不使用任何'Controller'代碼,所以我還會移除'require_relative'controller''。 – 2015-03-02 17:13:45