I’m writing a program in Ruby for the first time (I’m using RubyMine as my IDE), having previously coded mostly in Java.
When programming in Java, I used to regularly ‘run’ my code after each bit of functionality I had added to it, just to check that it worked properly.
I’m trying to do that with Ruby, but I’m having a little bit of difficulty.
I have the following class, which I will be using as the menu for my program, so it is where I want the user to start off when they run the program:
class Application
# To change this template use File | Settings | File Templates.
def initialize
mainMenu
end
def navigateTo(what)
what.new.display
mainMenu
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"
navigateTo Module
addModule
when "2"
navigateTo Module
when "3"
navigateTo Module
when "4"
navigateTo Module
when "5"
navigateTo Student
when "6"
navigateTo Student
when "7"
navigateTo Student
end
end
end
However, when I run the class, the console displays a line stating that it’s running it, but then the next line is “Process finished with exit code 0”
I can’t work out why this is? In Java, there was a main method, which was where the program would always go for instruction on what to do when it was run… but as far as I can tell with Ruby, there’s no need for a main method? If this is the case, how can I run what I’ve written so far to check that I’ve written it correctly?
*UPDATED**
Ok, I’ve added in the line
Application.new
as suggested, and that’s brilliant- the menu now prints out. I selected option 1 from the menu, and this line was printed out in the console:
#<Module:0x1bd2c90>What would you like to do?
followed by a printout of the menu again.
Option 1 should navigate to my Module class, which looks like this:
class Module
# To change this template use File | Settings | File Templates.
@@moduleScheme = nil
@@moduleYear = nil
#@moduleTitle = ""
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
def addModule
moduleName = Module.new(30)
moduleRefNo = Random(100)
#moduleTitle = @moduleTitle
moduleYear(4)
print "What is the name of the module you would like to add?"
moduleName = gets
moduleRefNo
printf "Which year does the module belong to?"
@@moduleYear = gets
puts "#{moduleName}, belonging to #{@@moduleYear} has been added to the system, with reference number #{moduleRefNo}."
navigateTo Application
end
def addModuleToScheme
moduleName.moduleScheme = schemeName
end
def removeModuleFromScheme
moduleName.moduleScheme = nil
end
def queryModule
end
end
Once the the user has selected option 1 from the main menu, and the program has navigated to the Module class, I expected it to run that class fully, i.e. display the prompts to the user, and read in whatever they type on the keyboard, then navigate back to the menu, as indicated by the line
navigateTo Application
at the end of my ‘addModule’ function. However, for some reason, it seems to either not be navigating to the Module class, or just skipping straight to the end of it. Can anyone point out what I’m doing wrong here?
When you run a ruby file, it will run the file from beginning to end. In your file, you are just defining a class, so it will create that class in ruby, then do nothing, since you are not telling it to instantiate a instance of that class. At the end of your file, add
Application.new, which will create an instance of that class, and, looking at your code, will print and receive input