Ruby未定义方法错误-我以为我已经定义了它



我正在用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"中得到了一个"undefined method"错误。它说它是为'nil:NilClass(NoMethodError)

有人能向我解释一下吗?我该怎么纠正呢?

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

最新更新