-1

我有以下代码,通过从我的程序的菜单中选择选项 1 来调用它:

def self.addModuleToScheme
=begin
Create an empty hash for the schemes
=end
 schemes = Hash.new()
=begin
Allow user to enter scheme names into a set of variables, and use each scheme name as a hash/ array of modules.
Then allow the user to enter the the modules for each scheme into each of the hashes

Create specific hash elements by using the following line:
schemes = {:scheme1 => scheme1variable, :scheme2 => scheme2variable}
=end

 puts "What is the name of the scheme that you would like to add a module to? "
 schemeName = gets
=begin
Need to use an if statement here to check whether or not the scheme already exists, if it doesn't, create it, if it does,
tell the user that it does.
=end
 if schemes.has_key?(schemeName)
   puts "This scheme has already been added "
 else
   schemes[@noOfModulesInScheme] = schemeName
 end

 @noOfModulesInScheme + 1
# moduleName.moduleScheme = schemeName

# Print to check that scheme has been added to system:
 if schemes.has_key?(schemeName)
   puts "@schemeName has been added to the system "
 end

目前,当我运行代码时,会显示菜单,我选择选项 1,即调用此代码的时间。然后我被问到我想添加模块的方案的名称是什么。我输入它,然后程序退出。

我想做的是打印输出,告诉我刚刚输入的方案已添加到系统中,然后我的程序继续询问我想添加到方案中的模块。

但是我不知道如何在句子中打印散列元素的内容。谁能指出我正确的方向?

编辑

如果有任何帮助,这是我程序中的另一个类(打印菜单的地方)

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.addModuleToScheme
    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: Class (NoMethodError) 的未定义方法'add_module'。

尽管事实上我从类中删除了旧方法(称为 addModuleToScheme),然后编写了新方法,更新了菜单中的方法调用,并保存了两个类。似乎我的菜单仍在尝试调用旧方法,即使它不再存在......有什么想法吗?

4

1 回答 1

0

您的代码有很多问题-

  1. 在方法名和变量名中使用 Un-Ruby 风格的语法。
  2. =begin; comment; =end即使是单行注释的使用也不好。就我个人而言,即使是多行注释,我也更喜欢使用 # 样式。
  3. 局部变量和实例变量的可互换使用。例如,在最后一行中,您在哈希键中搜索局部变量schemeName,随后puts使用实例变量@schemeName,尽管我怀疑您可能正在尝试实现插值效果,您需要使用哪个 btw"#{schemeName}"而不是"@schemeName"

但是,除此之外,如果我正确理解了您想要实现的目标(即创建一个方案哈希,其中方案作为键,模块作为值),然后尝试以下代码。在此示例中,我假设您希望方案和模块之间具有 1:1 的关系,但是如果您想为每个方案定义多个模块,您可以通过添加另一个循环并将方案的字符串值替换为数组来轻松实现在下面的代码中。

def add_module
    schemes = {}
    scheme_exists = false
    add_another_scheme = 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
                    print "Enter Module Name: "
                    module_name = gets
                    schemes[scheme_name.chop] = module_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

            print "Enter another scheme? "
            ask_user_if_would_like_to_add_another_scheme = gets
            if !(ask_user_if_would_like_to_add_another_scheme.chop == "Y" or ask_user_if_would_like_to_add_another_scheme.chop == "Yes")
                    add_another_scheme = false
            end
    end
    puts schemes
end
于 2012-08-21T10:50:25.547 回答