我正在使用 Ruby on Rails 编写一个程序,以允许用户管理大学课程、他们的模块以及注册这些课程的学生。
目前,我有两个类:Application.rb 和 CourseModules.rb:
Application.rb 是我用来与用户交互的类——它目前看起来像这样:
代码:
class Application
# To change this template use File | Settings | File Templates.
require './courseModules.rb'
def initialize
main_menu
end
=begin
def navigateTo(what)
what.new(v).display
mainMenu
end
=end
def main_menu
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"
CourseModules.add_module
when "2"
CourseModules.removeModuleFromScheme
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 = ""
@noOfModulesInScheme = 0
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
# Attempt at add_module method on 21/08/2012 at 16:30
def self.add_module
schemes = {}
scheme_exists = false
add_another_scheme = true
add_another_module = true
while add_another_scheme
print "Enter scheme name: "
scheme_name = gets
schemes.has_key?(scheme_name.chop) ? scheme_exists = true : scheme_exists = false
if !scheme_exists
schemes[scheme_name.chop] = []
puts "Scheme #{scheme_name.chop} has been added to the system"
else
scheme_exists = false
puts "This scheme has already been added"
end
while add_another_module
print "Enter module name: "
module_name = gets
schemes[scheme_name.chop].include?(module_name.chop) ? true : schemes[scheme_name.chop] << module_name.chop
print "Add another module? "
ask_if_user_wants_to_add_another_module = gets
if(ask_if_user_wants_to_add_another_module.chop == "y" or ask_if_user_wants_to_add_another_module == "yes")
add_another_scheme = false
else if(ask_if_user_wants_to_add_another_module.chop != "y" or ask_if_user_wants_to_add_another_module != "yes")
Application.main_menu
end
end
end
print "Add another scheme? "
ask_if_user_wants_to_add_another_scheme = gets
if !(ask_if_user_wants_to_add_another_scheme.chop == "y" or ask_if_user_wants_to_add_another_scheme.chop == "yes")
add_another_scheme = false
end
puts @schemes
end
while add_another_module
print "Enter scheme name: "
scheme_name = gets
schemes.has_key?(scheme_name.chop) ? scheme_exists = true : scheme_exists = false
if !scheme_exists
print "Enter module name: "
module_name = gets
schemes[scheme_name.chop] = module_name.chop
puts "Scheme #{scheme_name.chop} with module #{module_name} has been added to the system"
else
scheme_exists = false
puts "This scheme has already been added"
puts "Enter module name: "
module_name = gets
schemes[scheme_name.chop] = module_name.chop
puts "Scheme #{scheme_name.chop} with module #{module_name} has been added to the system"
end
print "Add another module? "
ask_if_user_wants_to_add_another_module = gets
if !(ask_if_user_wants_to_add_another_module.chop == "y" or ask_if_user_wants_to_add_another_module.chop == "yes")
add_another_module = false
end
end
puts schemes
end
end
目前,当我从 Application.rb 运行程序时,我会看到菜单,我选择选项 1:“将模块添加到方案”
然后我被要求输入一个方案名称,我这样做了,然后出现一条线,告诉我该方案已添加到系统中。
接下来,我被要求输入模块的名称,我这样做了,然后被问到是否要输入另一个模块。如果我输入“y”或“yes”,我可以输入另一个模块的名称。但是,如果我输入“n”或除“y”或“yes”以外的任何内容,程序就会崩溃,并且在控制台中会出现一些错误。
这些错误中的第一个说:
在
add_module': undefined method
main_menu' 中为 Application:Class (NoMethodError)
在线上:
Application.main_menu
在里面
def self.add_module
我的 CourseModules.rb 类中的方法。
应该发生的情况是,当用户在询问他们是否要添加另一个模块时输入“n”或“y”或“yes”以外的任何内容时,他们应该被收回程序主菜单。
有谁知道我在这里做错了什么?我怎样才能把它正确?