0

我正在用 Ruby 编写一个课程管理程序,以帮助管理大学的课程、与每门课程相关的模块以及在每个课程和模块上注册的学生。

每个学生只能注册一门课程(或计划),并且只能参加他们计划中可用的模块。

目前,我有两个类:Application.rb,这是我用作菜单的类,用于与用户交互,以及 CourseModules.rb,这是我正在做所有工作的类

应用程序.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

课程模块.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。

但是,然后我在“has_key”的“add_module”中得到一个“未定义的方法”错误。它说它是'nil:NilClass(NoMethodError)

谁可以给我解释一下这个?我该怎么说呢?

4

1 回答 1

0

您正在参考,@schemes但您将其定义为schemes = {},因此nil:NilClass (NoMethodError).

于 2012-08-21T20:18:00.287 回答