Note that there are some explanatory texts on larger screens.

plurals
  1. PORuby using variables in print statements
    primarykey
    data
    text
    <p>I have the following code, which is called by selecting option 1 from the menu in my program:</p> <pre><code>def self.addModuleToScheme =begin Create an empty hash for the schemes =end schemes = Hash.new() =begin Allow user to enter scheme names into a set of variables, and use each scheme name as a hash/ array of modules. Then allow the user to enter the the modules for each scheme into each of the hashes Create specific hash elements by using the following line: schemes = {:scheme1 =&gt; scheme1variable, :scheme2 =&gt; scheme2variable} =end puts "What is the name of the scheme that you would like to add a module to? " schemeName = gets =begin Need to use an if statement here to check whether or not the scheme already exists, if it doesn't, create it, if it does, tell the user that it does. =end if schemes.has_key?(schemeName) puts "This scheme has already been added " else schemes[@noOfModulesInScheme] = schemeName end @noOfModulesInScheme + 1 # moduleName.moduleScheme = schemeName # Print to check that scheme has been added to system: if schemes.has_key?(schemeName) puts "@schemeName has been added to the system " end </code></pre> <p>Currently, when I run the code, the menu is displayed, and I select option 1, which is when this code is called. Then I am asked what is the name of the scheme I would like to add the module to. I type that in, and then the program exits.</p> <p>What I would like to do, is have a printout telling me that the scheme that I have just entered has been added to the system, and for my program to then go on and ask me what module I would like to add to the scheme.</p> <p>But I can't work out how to print the contents of a hash element within a sentance. Can anyone point me in the right direction?</p> <p><strong>EDIT</strong></p> <p>If it's any help, this is the other class in my program (where the menu is printed from)</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.addModuleToScheme 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><strong>Edit</strong></p> <p>Ok, I've made the changes suggested, however, when I now run my program, I'm getting an error saying "undefined method 'add_module' for CourseModules: Class (NoMethodError).</p> <p>This is despite the fact that I deleted the old method I had (called addModuleToScheme) from the class, then wrote the new one, updated the method call in the menu, and saved both classes. It seems like my menu is still trying to call the old method even though it's no longer there... any ideas?</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