I have the following class, which I will be using as the menu for my program:
class Application
# To change this template use File | Settings | File Templates.
def initialize
mainMenu
end
def navigateTo(what)
what.new.display
mainMenu
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"
navigateTo Module
addModule
when "2"
navigateTo Module
when "3"
navigateTo Module
when "4"
navigateTo Module
when "5"
navigateTo Student
when "6"
navigateTo Student
when "7"
navigateTo Student
end
end
Application.new
end
However, when I run the class, I tried selecting option 1 from the menu, and this line was printed out in the console:
#<Module:0x1bd2c90>What would you like to do?
followed by a printout of the menu again.
Option 1 should navigate to my Module class, which looks like this:
class Module
# To change this template use File | Settings | File Templates.
@@moduleScheme = nil
@@moduleYear = nil
#@moduleTitle = ""
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
moduleName.moduleScheme = schemeName
end
def removeModuleFromScheme
moduleName.moduleScheme = nil
end
def queryModule
end
end
Once the the user has selected option 1 from the main menu, and the program has navigated to the Module class, I expected it to run that class fully, i.e. display the prompts to the user, and read in whatever they type on the keyboard, then navigate back to the menu, as indicated by the line
navigateTo Application
at the end of my 'addModule' function. However, for some reason, it seems to either not be navigating to the Module class, or just skipping straight to the end of it. Can anyone point out what I'm doing wrong here?
Edit 16/08/2012 at 14:53
Ok, so I've made the changes suggested and have altered the following function to include the (v):
def navigateTo(what)
what.new(v).display
mainMenu
end
However, I'm now getting a list of errors:
- (Application class filepath) in 'mainMenu' uninitialised constant Application: :CourseModules (nameError)
This occurs on the line navigateTo CourseModules under when "1" in the menu.
There's then another error on the line
mainMenuin the initialize method, which just says from (filepath) in 'initialize'A similar one which just says from (filepath) 'new' on the line
Application.newat the end of the Application.rb class, along with one that says in 'class:Application' on the same lineThen I have one on the first line that says (filepath) in 'top (required)'
Finally, I have another two error which are quite different to the previous ones:
from -e:1:in 'load'
from -e:1:in 'main'
Any other ideas about what I'm now doing wrong?
*Edit 16/08/2012 at 16:15 *
My full script for my Application.rb class is:
class Application
# To change this template use File | Settings | File Templates.
def initialize
mainMenu
end
def navigateTo(what)
what.new(v).display
mainMenu
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"
navigateTo CourseModules
when "2"
navigateTo CourseModules
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
and the full script for my courseModules.rb class is:
class CourseModules
# To change this template use File | Settings | File Templates.
@@moduleScheme = nil
@@moduleYear = nil
#@moduleTitle = ""
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
moduleName.moduleScheme = schemeName
end
def removeModuleFromScheme
moduleName.moduleScheme = nil
end
def queryModule
end
end
class Moduleis a baaad idea.require_relative 'course_module'