Note that there are some explanatory texts on larger screens.

plurals
  1. PORuby- saving user input
    text
    copied!<p>I am writing a course management program in Ruby to allow a user to to add/ remove modules to a scheme.</p> <p>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.</p> <p>The method I'm using to add the modules is: </p> <pre><code>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] &lt;&lt; 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] &lt;&lt; $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] &lt;&lt; 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 </code></pre> <p>and the method I'm using to try to remove the module is: </p> <pre><code>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 </code></pre> <p>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:</p> <ol> <li>Enter scheme name -- A message is displayed stating that the scheme has been added to the system </li> <li>Enter module name</li> <li>Enter module ID</li> <li>Enter academic year to which the module belongs -- A message is displayed stating that the module has been added to the scheme for that year </li> <li>I'm asked if I want to add another module, so I say yes</li> <li>The program runs through the same steps again, but this time skipping the first one, and starting at entering the module name -- When I've followed through the steps again, another message is displayed, stating that the second module has been added to the same scheme for whatever year I specified the second time</li> <li>I'm then asked if I want to add another module, to which I type 'n', and am returned to the original menu.</li> <li>This time I select the option to remove a module from a scheme</li> <li>I'm asked which scheme I would like to remove a module from, so I enter the one I'd added the modules to</li> <li>I'm then asked which module I would like to remove, so I enter one of the ones I had previously added to it, but I'm then told that that module does not exist in the scheme.</li> </ol> <p>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).</p> <p>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?</p> <p>Any help would be much appreciated!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload