我正在编写我的第一个 Ruby on Rails 程序,这是一个帮助管理/组织大学课程、参加课程的学生以及属于每个课程的模块的应用程序。
我目前有两个类:第一个类,称为 Application,是我用来与用户交互的类,如下所示:
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 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
def addModule
moduleName = Module.new(30)
moduleRefNo = Random(100)
#moduleTitle = @moduleTitle
moduleYear(4)
print "What is the name of the module you would like to add?"
moduleName = gets
moduleRefNo
printf "Which year does the module belong to?"
@@moduleYear = gets
puts "#{moduleName}, belonging to #{@@moduleYear} has been added to the system, with reference number #{moduleRefNo}."
navigateTo Application
end
def addModuleToScheme
# Create an empty hash for the schemes
schemes = Hash.new()
# 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}
puts "What is the name of the scheme that you would like to add a module to? "
schemeName = gets
# 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.
if schemes.has_key?(schemeName)
puts "This scheme has already been added "
else
schemes.add(schemeName)
end
noOfModulesInScheme + 1
moduleName.moduleScheme = schemeName
end
def removeModuleFromScheme
moduleName.moduleScheme = nil
end
def queryModule
end
end
目前,我正在处理菜单中的第一个选项 - “将模块添加到方案”。当我尝试从 Application.rb 类运行程序时,会显示菜单,我输入:“1”,这是我要选择的选项。
但是,然后我收到一条错误消息,说我的方法“addModuleToScheme”未定义。有人可以指出我的“addModuleToScheme”方法做错了什么吗?
非常感谢。
编辑 21/08/2012 00:35
好的,我认为这似乎已经解决了我的 addModuleToScheme 方法,但是当我尝试将方案名称实际添加到哈希时出现错误:
当我现在从菜单中选择“1”时,系统会要求我输入方案的名称,但是,当我输入该名称时,会出现未定义的方法错误,
undefined method 'add' for {}:Hash (NoMethodError)"
在线上
schemes.add(schemeName)
在我的 if else 语句中。有什么想法我在这里做错了吗?