I am writing a program using Ruby on Rails to allow users to manage University courses, their modules, and the students enrolled on them.
Currently, I have two classes: Application.rb and CourseModules.rb:
Application.rb is the class that I am using to interface with the user- it currently looks like this:
Code:
class Application
# To change this template use File | Settings | File Templates.
require './courseModules.rb'
def initialize
main_menu
end
=begin
def navigateTo(what)
what.new(v).display
mainMenu
end
=end
def main_menu
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.add_module
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
and CourseModules.rb is where I am doing all of the work- it currently looks like this:
Code:
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
# Attempt at add_module method on 21/08/2012 at 16:30
def self.add_module
schemes = {}
scheme_exists = false
add_another_scheme = true
add_another_module = 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
schemes[scheme_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
while add_another_module
print "Enter module name: "
module_name = gets
schemes[scheme_name.chop].include?(module_name.chop) ? true : schemes[scheme_name.chop] << module_name.chop
print "Add another module? "
ask_if_user_wants_to_add_another_module = gets
if(ask_if_user_wants_to_add_another_module.chop == "y" or ask_if_user_wants_to_add_another_module == "yes")
add_another_scheme = false
else if(ask_if_user_wants_to_add_another_module.chop != "y" or ask_if_user_wants_to_add_another_module != "yes")
Application.main_menu
end
end
end
print "Add another scheme? "
ask_if_user_wants_to_add_another_scheme = gets
if !(ask_if_user_wants_to_add_another_scheme.chop == "y" or ask_if_user_wants_to_add_another_scheme.chop == "yes")
add_another_scheme = false
end
puts @schemes
end
while add_another_module
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} with module #{module_name} has been added to the system"
else
scheme_exists = false
puts "This scheme has already been added"
puts "Enter module name: "
module_name = gets
schemes[scheme_name.chop] = module_name.chop
puts "Scheme #{scheme_name.chop} with module #{module_name} has been added to the system"
end
print "Add another module? "
ask_if_user_wants_to_add_another_module = gets
if !(ask_if_user_wants_to_add_another_module.chop == "y" or ask_if_user_wants_to_add_another_module.chop == "yes")
add_another_module = false
end
end
puts schemes
end
end
At present, when I run the program from Application.rb, I am presented with the menu, and I choose option 1: "Add module to a scheme"
I am then asked to enter a scheme name, which I do, and a line appears telling me that the scheme has been added to the system.
Next, I am asked to enter the name of the module, which I do, and am then asked if I'd like to enter another module. If I type 'y' or 'yes', I am allowed to enter the name of another module. However, if I type 'n' or anything other than 'y' or 'yes', the program crashes and I get a few errors in the console.
The first of these errors says:
in
add_module': undefined method
main_menu' for Application:Class (NoMethodError)
on the line:
Application.main_menu
in the
def self.add_module
method in my CourseModules.rb class.
What should happen is that when the user enters 'n' or anything other than 'y' or 'yes' when asked if they'd like to add another module, they should be taken back the program main menu.
Does anyone have any idea what I'm doing wrong here? How can I put it right?
It looks like you are trying to call a class method with Application.main_menu when it is defined as an instance method on the Application class which is why you are getting an undefined_method. Try adding self to the main_menu method definition.
class Application
def self.main_menu
....
end
end
Fix based on your comments:
You will need to modify the initialize method as well. You can do so by changing it to this
class Application
def initialize
self.class.main_menu
end
...
end
By adding just self before main_menu in the initialize method you are just telling initialize that you want to initialize a new method when you create a new Application class. In order to tell initialize that you want this method to be a class method you need to pass in the class as well. I hope this makes sense.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.