Ruby程序-创建菜单
我有以下课程,我将使用它作为我的程序的菜单:Ruby程序-创建菜单,ruby,Ruby,我有以下课程,我将使用它作为我的程序的菜单: 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 li
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,并且程序导航到模块类,我希望它完全运行该类,即向用户显示提示,并读入他们在键盘上键入的任何内容,然后导航回菜单,如行所示
navigateTo Application
在我的“addModule”函数的末尾。然而,出于某种原因,它似乎不是导航到模块类,就是直接跳到模块类的末尾。有人能指出我做错了什么吗
编辑2012年8月16日14:53
好的,我做了建议的修改,修改了以下函数以包含(v):
但是,我现在得到了一个错误列表:
下的导航到课程模块的行中
然后,initialize方法中的main菜单
行出现另一个错误,它在'initialize'中只显示from(filepath)
在Application.rb类的末尾,在Application.rb类的第行Application.new
中仅显示from(filepath)“new”,在同一行的“class:Application”中显示
然后我在第一行有一个在“top(required)”中写着(filepath)
最后,我还有另外两个错误,与前面的错误截然不同:
from-e:1:在“加载”中
from-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
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
我的courseModules.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
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
应用程序从不显示CourseModules菜单(即从不调用addModule
)的原因是它被卡在application.main菜单方法中。按“1”时,将调用应用程序.navigateTo
方法。这将输出CourseModules对象,即您看到的#
,然后再次显示主菜单。你永远都在这样做,这就是为什么你从来没有到达addModule行
代码中有很多问题,所以我不确定如何建议修复。比如说,
1) what.new.display
将失败,因为CourseModules initialize方法需要1个参数,但您没有传入任何参数
2) 由于应用程序类中不存在该方法,因此要从主菜单方法调用的addModule
方法将失败
3) addModule
方法将失败,因为CourseModules类不存在navigateTo方法
我认为您需要重新评估如何组织代码。可能将所有与接口相关的方法放在一个类中。该类将处理显示提示和将任何输入传递给适当的类进行处理。类模块
是一个baaad想法。您会推荐什么替代方法?选择另一个名称。标准libraryOk中已经有一个同名的类,因此我将该类重命名为CourseModules,并在我的应用程序类中更改了对该类的所有引用。但是,当我现在尝试运行应用程序类,并从菜单中选择选项1时,我得到一个nameError。。。你知道我该怎么纠正吗?也许你忘了要它了<代码>需要相关的“课程模块”
谢谢您的回复。那么关于第1点,我会写[code]what.new.display(initialize)[/code]引用CourseModules类中的initialize方法吗?应该是what.new(v.display
,其中v是您希望@val在CourseModules类中等于的值。好的,我已经做了您建议的更改,但是,当我现在运行代码时,我有几个错误-我更新了我原来的帖子来显示新的错误。你能把你的完整脚本吗?有相当多的变化,所以我想确保我使用的是你更新的代码。我现在发布了我的完整脚本。谢谢你的帮助。