Note that there are some explanatory texts on larger screens.

plurals
  1. PORuby program exits too soon, data being overwritten
    primarykey
    data
    text
    <p>I'm writing a Ruby program to simulate a course management system. As my program currently stands, when the user first loads the program, they are presented with a menu. I am currently just working on the first option in the menu- which is to add a module to a scheme.</p> <p>Working through this step by step, when the user selects this option from the menu, they are asked to enter a scheme name, which is saved as the key in a hash called 'schemes'. They are then asked to enter a module name, which is saved to the hash, under the key which belongs to the scheme they just entered.</p> <p>A line is then printed out telling the user that the scheme and the module have been added to the system.</p> <p>They are then asked if they would like to add another module. This is where I have a couple of questions:</p> <p>A. If the user types 'y', they are asked to enter the scheme- if the scheme they enter already exists, they are told, if not, it is created, and they are then asked for the module name. Then they are asked if they would like to add another module. If the user types 'n' the schemes that currently exist, along with their modules are printed out, and the program exits.</p> <p>B. In this case, I had added two modules to the same scheme, but only the last one was printed out, so presumably, it is overriding the first one that was entered- how can I make sure that this doesn't happen, and that after the first module has been saved to the 'schemes' hash, any subsequent ones are appended to the end of the hash, and not replacing the one already there?</p> <ol> <li><p>How can I make sure that the program doesn't exit when the user types 'n' at step A, but rather returns to the main program menu?</p></li> <li><p>How can I make sure that the first piece of data saved to a hash is not overridden by any subsequent pieces of data that I save to the hash, but rather that they become separate entries? (The idea here being to have something similar to a Java ArrayList, that will automatically add the next bit of data to the next array element that is free- increasing numerically)</p></li> </ol> <p>The two classes I have are Application (which is acting as my interface with the user), and CourseModules (which is where the data entered by the user is stored).</p> <p>Application.rb currently looks like this:</p> <pre><code>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.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 </code></pre> <p>and CourseModules.rb currently looks like this:</p> <pre><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 12:35 def self.add_module schemes = {} scheme_exists = false add_another_module = true 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 def removeModuleFromScheme moduleName.moduleScheme = nil end def queryModule end </code></pre> <p>*<strong><em>Edit 21/08/2012 at 18:00</em>**</strong></p> <p>Ok, my courseModules class now looks like this:</p> <pre><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] &lt;&lt; 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 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 </code></pre> <p>but now, when I select '1' from the menu, and enter a scheme name, I'm getting an error on the line:</p> <pre><code>@schemes.has_key?(scheme_name.chop) ? scheme_exists = true : scheme_exists = false </code></pre> <p>which says </p> <blockquote> <p>in <code>add_module': undefined method</code>has_key?' for nil:NilClass (NoMethodError)</p> </blockquote> <p>Does anyone know what I can do to resolve this?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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