我正在编写我的第一个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语句中。我在这里做错了什么想法?
答案 0 :(得分:2)
我打赌它告诉你CourseModules.addModuleToScheme
未定义。这是因为当您尝试将该方法用作类方法时,已将该方法实现为实例方法。
尝试将其定义为:
def self.addModuleToScheme
# ... do stuff
end
或尝试将其用作实例方法,在这种情况下,您必须首先创建CourseModules
类的新实例。