我有以下代码,通过从我的程序的菜单中选择选项 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),然后编写了新方法,更新了菜单中的方法调用,并保存了两个类。似乎我的菜单仍在尝试调用旧方法,即使它不再存在......有什么想法吗?