【问题标题】:Ruby- saving user inputRuby-保存用户输入
【发布时间】:2012-08-23 01:29:27
【问题描述】:

我正在用 Ruby 编写一个课程管理程序,以允许用户在方案中添加/删除模块。

目前,我的程序将允许用户添加模块,但是当我尝试删除它们时,我被告知它们不存在。

我用来添加模块的方法是:

def self.add_module
# schemes = {}
 scheme_exists = false
 add_another_scheme = true
# module_exists = false
 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"
   elsif
     scheme_exists = true
     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
    # puts "Module #{module_name.chop} has been added to #{scheme_name.chop}"

     # 22/08/2012 at 14:15 Now need to read in each module's unique identifier and year it belongs to
     print "Enter module ID: "
     $module_ID =gets
     $schemes[scheme_name.chop].include?($module_ID.chop) ? true : $schemes[scheme_name.chop] << $module_ID.chop
     $schemes.has_key?($module_ID.chop) ? module_exists = true : module_exists = false

     print "Enter the academic year to which the module belongs: "
     module_year = gets
     $schemes[scheme_name.chop].include?(module_year.chop) ? true : $schemes[scheme_name.chop] << module_year.chop

     if !$module_exists
       $schemes[$module_ID.chop] = []
       puts "Module #{$module_ID.chop} : #{module_name.chop} has been added to #{scheme_name.chop} for the year #{module_year}"
     elsif
       $module_exists = true
       puts "A module with this ID has already been added to the scheme, please check if the module already exists, or choose another ID "
     else
     #  puts "Module #{module_name.chop}, #{module_ID.chop} has been added to #{scheme_name.chop} for the year #{module_year}"
     end

    # puts "Module #{module_name.chop}, #{module_ID.chop} has been added to #{scheme_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

我用来尝试删除模块的方法是:

def self.remove_module

 print "Which scheme would you like to remove a module from? "
 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} doesn't exist"
 else
 scheme_exists = true
   puts "Which module would you like to remove from #{scheme_name.chop}?"
   $module_ID = gets
   if !$module_exists
     $schemes[$module_ID.chop] = []
     puts "Module #{$module_ID.chop} : does not exist in #{scheme_name.chop} "
   else
     module_exists = true
     puts "Module #{$module_ID.chop} has been removed from #{scheme_name.chop} "
   #  puts "Module #{module_name.chop}, #{module_ID.chop} has been added to #{scheme_name.chop} for the year #{module_year}"
 end
 end

end

当我运行程序时,会显示一个菜单,然后我选择将一个模块添加到一个调用第一个方法的方案中。我按照以下步骤操作:

  1. 输入方案名称 -- 显示一条消息,说明该方案已添加到系统中
  2. 输入模块名称
  3. 输入模块 ID
  4. 输入模块所属的学年 -- 显示一条消息,说明该模块已添加到该年的方案中
  5. 有人问我是否要添加另一个模块,所以我说是
  6. 程序再次执行相同的步骤,但这次跳过第一个步骤,并从输入模块名称开始 -- 当我再次执行这些步骤时,会显示另一条消息,说明第二个模块已添加到我第二次指定的任何年份的同一方案中
  7. 然后我被询问是否要添加另一个模块,我在其中输入“n”,然后返回到原始菜单。
  8. 这次我选择从方案中删除模块的选项
  9. 有人问我要从哪个方案中删除模块,所以我输入了我添加模块的方案
  10. 然后有人问我要删除哪个模块,所以我输入了我之前添加到其中的一个模块,但随后我被告知该模块在方案中不存在。

这表明我在调用第一种方法(添加模块)时存储在变量中的数据在我调用第二种方法(删除模块)时刚刚被丢弃。

如何确保这些信息不会丢失?是否有我需要设置和连接我的程序的数据库,或者我需要使用会话和会话变量?还是完全不同?

任何帮助将不胜感激!

【问题讨论】:

    标签: ruby


    【解决方案1】:

    (不是答案。)

    我在阅读您的代码时遇到问题。例如:

    $schemes.has_key?(scheme_name.chop) ? scheme_exists = true : scheme_exists = false
    # Did you mean:
    scheme_exists = $schemes.has_key?(scheme_name.chop)
    

    还有:

    if !scheme_exists
      $schemes[scheme_name.chop] = []
      puts "Scheme #{scheme_name.chop} doesn't exist"
    else
      scheme_exists = true
      # ...
    

    为什么将scheme_exists 设置为true?您刚刚测试了它不是正确的。

    您的“方案存在”看起来很像您的“模块存在”,如果它们相同,则使它们相同。有很多 chopping 正在进行,也许您应该在输入后切碎并停止在其他任何地方切碎——这似乎容易出错并增加了很多噪音。

    总的来说,我发现如果不实际单步执行您的代码就很难推理。我建议进行重构,让事情像您要解决的问题一样“大声”读出。

    我们也不知道是否还有其他内容涉及$schemes。您是否“应该”使用数据库完全取决于您的目标、您的约束、您运行应用程序的方式等。您还可以序列化 YAML、编写纯文本文件等等各种东西。

    如果您重新启动/重新运行应用程序,您显然会丢失所有未保存的数据。如果您一直呆在应用程序中,那么很可能代码或您的假设是错误的,但由于代码的结构和命名方式,查看代码并确定是一项繁重的任务。

    【讨论】:

    • 回复。代码的第一部分-$schemes 是我存储在用户调用“添加模块”方法时添加的方案的数组。如果该方案尚未添加 - 我需要让用户知道,因为没有要删除的关联模块,否则(即如果该方案已添加),那么我需要询问用户与该方案关联的模块他们想删除。
    • 回复。代码的第二部分 - scheme_exists 类似于 module_exists ,只是它检查方案是否存在,而不是模块是否存在......方案和模块是两个不同的东西。
    • @Someone2088 代码相同,只是地图不同。相同的代码。您不一定需要相信或理解我在说什么,但最终如果您希望其他人能够阅读您的代码,您需要进行相当多的更改。目前真的很难摸索。
    • 抱歉,地图是什么意思?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-21
    • 2015-09-26
    • 2018-07-22
    • 2020-10-30
    • 2023-04-02
    • 2021-11-11
    • 1970-01-01
    相关资源
    最近更新 更多