Reputation: 141
I am writing a course management program in Ruby to allow a user to to add/ remove modules to a scheme.
Currently, my program will allow the user to add modules, but when I then try to remove them, I am told that they don't exist.
The method I'm using to add the modules is:
def self.add_module
# schemes = {}
scheme_exists = false
add_another_scheme = true
# module_exists = false
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"
elsif
scheme_exists = true
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
# puts "Module #{module_name.chop} has been added to #{scheme_name.chop}"
# 22/08/2012 at 14:15 Now need to read in each module's unique identifier and year it belongs to
print "Enter module ID: "
$module_ID =gets
$schemes[scheme_name.chop].include?($module_ID.chop) ? true : $schemes[scheme_name.chop] << $module_ID.chop
$schemes.has_key?($module_ID.chop) ? module_exists = true : module_exists = false
print "Enter the academic year to which the module belongs: "
module_year = gets
$schemes[scheme_name.chop].include?(module_year.chop) ? true : $schemes[scheme_name.chop] << module_year.chop
if !$module_exists
$schemes[$module_ID.chop] = []
puts "Module #{$module_ID.chop} : #{module_name.chop} has been added to #{scheme_name.chop} for the year #{module_year}"
elsif
$module_exists = true
puts "A module with this ID has already been added to the scheme, please check if the module already exists, or choose another ID "
else
# puts "Module #{module_name.chop}, #{module_ID.chop} has been added to #{scheme_name.chop} for the year #{module_year}"
end
# puts "Module #{module_name.chop}, #{module_ID.chop} has been added to #{scheme_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
and the method I'm using to try to remove the module is:
def self.remove_module
print "Which scheme would you like to remove a module from? "
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} doesn't exist"
else
scheme_exists = true
puts "Which module would you like to remove from #{scheme_name.chop}?"
$module_ID = gets
if !$module_exists
$schemes[$module_ID.chop] = []
puts "Module #{$module_ID.chop} : does not exist in #{scheme_name.chop} "
else
module_exists = true
puts "Module #{$module_ID.chop} has been removed from #{scheme_name.chop} "
# puts "Module #{module_name.chop}, #{module_ID.chop} has been added to #{scheme_name.chop} for the year #{module_year}"
end
end
end
When I run the program, a menu is displayed, and I select to add a module to a scheme, which calls the first method. I follow through the steps:
This suggests to me that the data I had stored in the variables when I called the first method (to add modules) has just been discarded when I called the second method (to remove modules).
How do I make sure that this information is not lost? Is there a database that I need to set up and connect my program to, or do I need to be using sessions and session variables? Or is it something completely different?
Any help would be much appreciated!
Upvotes: 1
Views: 153
Reputation: 160191
(Not an answer.)
I'm having problems reading your code. For example:
$schemes.has_key?(scheme_name.chop) ? scheme_exists = true : scheme_exists = false
# Did you mean:
scheme_exists = $schemes.has_key?(scheme_name.chop)
And:
if !scheme_exists
$schemes[scheme_name.chop] = []
puts "Scheme #{scheme_name.chop} doesn't exist"
else
scheme_exists = true
# ...
Why do you set scheme_exists
to true? You just tested that it wasn't true.
Your "scheme exists" looks a lot like your "module exists", if they're the same, make them the same. There is a lot of chop
ping going on, perhaps you should just chop after input and stop chopping everywhere else–it seems error-prone and adds a lot of noise.
Overall I find it difficult to reason about your code without actually stepping through it. I'd recommend refactoring, making things read "out loud" like the problem you're trying to solve.
We also don't know if anything else touches $schemes
. Whether or not you "should" be using a database depends entirely on your goals, your constraints, how you're running the app, etc. You could also serialize YAML, write a plain text file, all sorts of things.
If you restart/rerun the app you'll obviously lose all your data that isn't persisted. If you stay within the app the whole time, then it's likely either the code or your assumptions are wrong, but it's an onerous task for someone to look over the code and determine because of the way it's structured and named.
Upvotes: 1