我有以下课程,我将使用它作为我的程序的菜单:
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 应该导航到我的 Module 类,如下所示:
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
但是,我现在得到一个错误列表:
- (应用程序类文件路径)在'mainMenu'未初始化的常量应用程序::CourseModules(nameError)
这发生在菜单navigateTo CourseModules
下方的行上。when "1"
然后在初始化方法的行上有另一个错误
mainMenu
,它只是说 from (filepath) in 'initialize'一个类似的,它只是
Application.new
在 Application.rb 类末尾的行中从 (filepath) 'new' 中说,以及在同一行的 'class:Application' 中说的然后我在第一行有一个在“顶部(必需)”中显示(文件路径)
最后,我还有另外两个与前面的完全不同的错误:
从 -e:1:in '加载'
从 -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