プログラムのメニューとして使用する次のクラスがあります。
class Application
# To change this template use File | Settings | File Templates.
def initialize
mainMenu
end
def navigateTo(what)
what.new.display
mainMenu
end
def mainMenu
puts "What would you like to do?
1: Add module to a scheme
2: Remove module from a scheme
3: Query modules
4: Modify module
5: Register a student on a scheme
6: Remove a student from a scheme
7: Register a student on a module
8: Remove a student from a module"
case gets.strip
when "1"
navigateTo Module
addModule
when "2"
navigateTo Module
when "3"
navigateTo Module
when "4"
navigateTo Module
when "5"
navigateTo Student
when "6"
navigateTo Student
when "7"
navigateTo Student
end
end
Application.new
end
ただし、クラスを実行するときに、メニューからオプション 1 を選択しようとすると、次の行がコンソールに出力されました。
#<Module:0x1bd2c90>What would you like to do?
その後、再度メニューを印刷します。
オプション 1 は、次のようなモジュール クラスに移動する必要があります。
class Module
# To change this template use File | Settings | File Templates.
@@moduleScheme = nil
@@moduleYear = nil
#@moduleTitle = ""
def self.moduleYear
@@moduleYear
end
def initialize(v)
@val = v
end
# Set and get the @val object value
def set (v)
@val = v
end
def get
return @val
end
def addModule
moduleName = Module.new(30)
moduleRefNo = Random(100)
#moduleTitle = @moduleTitle
moduleYear(4)
print "What is the name of the module you would like to add?"
moduleName = gets
moduleRefNo
printf "Which year does the module belong to?"
@@moduleYear = gets
puts "#{moduleName}, belonging to #{@@moduleYear} has been added to the system, with reference number #{moduleRefNo}."
navigateTo Application
end
def addModuleToScheme
moduleName.moduleScheme = schemeName
end
def removeModuleFromScheme
moduleName.moduleScheme = nil
end
def queryModule
end
end
ユーザーがメイン メニューからオプション 1 を選択し、プログラムが Module クラスに移動すると、そのクラスが完全に実行される、つまり、ユーザーにプロンプトが表示され、ユーザーがキーボードで入力したものを読み取ると予想しました。次に、行で示されているように、メニューに戻ります
navigateTo Application
私の「addModule」関数の最後に。ただし、何らかの理由で、Module クラスに移動していないか、最後までスキップしているようです。ここで私が間違っていることを誰かが指摘できますか?
2012 年 8 月 16 日 14:53 に編集
わかりましたので、提案された変更を行い、次の関数を変更して (v) を含めました。
def navigateTo(what)
what.new(v).display
mainMenu
end
ただし、エラーのリストを取得しています。
- (Application class filepath) in 'mainMenu' uninitialized constant Application: :CourseModules (nameError)
これは、メニューのnavigateTo CourseModules
下の行で発生しwhen "1"
ます。
次に、initialize メソッドの行に別のエラーがあり
mainMenu
、'initialize' の from (filepath) とだけ書かれています。Application.new
Application.rb クラスの末尾の行に from (filepath) 'new' とだけ書かれている同様のものと、同じ行に 'class:Application' と書かれているものがあります。次に、「トップ(必須)」の(ファイルパス)という最初の行に1つあります
最後に、以前のものとはまったく異なる別の 2 つのエラーがあります。
-e:1:in 'load' から
-e:1:in 'main' から
私が今間違っていることについて他のアイデアはありますか?
* 2012 年 8 月 16 日 16:15 に編集 *
Application.rb クラスの完全なスクリプトは次のとおりです。
class Application
# To change this template use File | Settings | File Templates.
def initialize
mainMenu
end
def navigateTo(what)
what.new(v).display
mainMenu
end
def mainMenu
puts "What would you like to do?
1: Add module to a scheme
2: Remove module from a scheme
3: Query modules
4: Modify module
5: Register a student on a scheme
6: Remove a student from a scheme
7: Register a student on a module
8: Remove a student from a module"
case gets.strip
when "1"
navigateTo CourseModules
when "2"
navigateTo CourseModules
when "3"
navigateTo CourseModules
when "4"
navigateTo CourseModules
when "5"
navigateTo Student
when "6"
navigateTo Student
when "7"
navigateTo Student
end
end
Application.new
end
そして、私の courseModules.rb クラスの完全なスクリプトは次のとおりです。
class CourseModules
# To change this template use File | Settings | File Templates.
@@moduleScheme = nil
@@moduleYear = nil
#@moduleTitle = ""
def self.moduleYear
@@moduleYear
end
def initialize(v)
@val = v
end
# Set and get the @val object value
def set (v)
@val = v
end
def get
return @val
end
def addModule
moduleName = Module.new(30)
moduleRefNo = Random(100)
#moduleTitle = @moduleTitle
moduleYear(4)
print "What is the name of the module you would like to add?"
moduleName = gets
moduleRefNo
printf "Which year does the module belong to?"
@@moduleYear = gets
puts "#{moduleName}, belonging to #{@@moduleYear} has been added to the system, with reference number #{moduleRefNo}."
navigateTo Application
end
def addModuleToScheme
moduleName.moduleScheme = schemeName
end
def removeModuleFromScheme
moduleName.moduleScheme = nil
end
def queryModule
end
end