Ruby程序退出太快,数据被覆盖

时间:2012-08-21 13:50:29

标签: ruby

我正在编写一个Ruby程序来模拟课程管理系统。正如我的程序目前所处,当用户首次加载程序时,它们会显示一个菜单。我目前正在处理菜单中的第一个选项 - 即将模块添加到方案中。

逐步完成此操作后,当用户从菜单中选择此选项时,系统会要求他们输入方案名称,该名称将作为密钥保存在名为“scheme”的哈希中。 然后要求他们输入一个模块名称,该名称保存在哈希中,属于他们刚刚输入的方案的密钥。

然后打印一行,告诉用户该方案和模块已添加到系统中。

然后询问他们是否想要添加另一个模块。这是我有几个问题的地方:

一个。如果用户输入'y',则要求他们输入方案 - 如果他们输入的方案已经存在,则告知他们,如果没有,则创建它,然后询问他们是否有模块名称。然后询问他们是否想要添加另一个模块。如果用户键入'n',则打印出当前存在的方案及其模块,并退出程序。

B中。在这种情况下,我在相同的方案中添加了两个模块,但只打印出最后一个模块,因此可能会覆盖输入的第一个模块 - 如何确保不会发生这种情况,并且在第一个模块保存到'scheme'哈希之后,任何后续的模块都被附加到哈希的末尾,而不是替换已经存在的那个?

  1. 如果用户在步骤A输入“n”而不是返回主程序菜单,如何确保程序不会退出?

  2. 如何确保保存到哈希的第一段数据不被我保存到哈希的任何后续数据覆盖,而是它们成为单独的条目? (这里的想法是有类似于Java ArrayList的东西,它会自动将下一位数据添加到下一个数字元素中自由增加的数据元素)

  3. 我拥有的两个类是Application(作为我与用户的接口)和CourseModules(存储用户输入的数据的地方)。

    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 12:35
    def self.add_module
      schemes = {}
      scheme_exists = false
      add_another_module = true
    
     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
     def removeModuleFromScheme
       moduleName.moduleScheme = nil
     end
    
     def queryModule
    
    end
    

    * 编辑21/08/2012 18:00 **

    好的,我的courseModules类现在看起来像这样:

    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
    

    但现在,当我从菜单中选择“1”并输入方案名称时,我收到错误:

    @schemes.has_key?(scheme_name.chop) ? scheme_exists = true : scheme_exists = false
    

      

    add_module': undefined method has_key?' for nil:NilClass(NoMethodError)

    有谁知道我能做些什么来解决这个问题?

1 个答案:

答案 0 :(得分:0)

正如我在另一个主题上告诉你的那样,如果你需要在方案和模块之间建立1:M关系,你需要在模块中存储模块。您还需要将方案哈希重新定义为实例变量。即在

def initialize
   @schemes = {}
   # ... your other code
end

def self.add_module
    scheme_exists = false
    add_another_scheme = true

    while add_another_scheme
        add_another_module = true 
        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.chop == "yes")
               add_another_module = 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
    end
    puts @schemes
end