我正在用Ruby编写课程管理课程,以协助管理大学课程,与每门课程相关的模块,以及在每门课程和模块上注册的学生。
每个学生只能注册一门课程(或计划),并且只能参加他们计划中可用的模块。
目前,我有两个类:Application.rb,它是我用作菜单的类,与用户交互,以及CourseModules.rb,这是我正在做的所有类工作
application.rb中:
class Application
# To change this template use File | Settings | File Templates.
require './courseModules.rb'
def initialize
mainMenu
end
=begin
def navigateTo(what)
what.new(v).display
mainMenu
end
=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"
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
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(将模块添加到方案中)。 然后我被要求输入方案名称,我这样做,然后按Enter键。
然而,我在'add_module'中为'has_key'得到一个'未定义的方法'错误。它说它是'nil:NilClass(NoMethodError)
有人可以向我解释一下吗?我该怎么说呢?
答案 0 :(得分:0)
您正在引用@schemes
,但您将其定义为schemes = {}
,因此nil:NilClass (NoMethodError)
。