2012-08-14 44 views
1

我第一次在Ruby中编写程序(我使用RubyMine作为我的IDE),以前主要使用Java进行编码。在开发期间运行Ruby程序

当用Java编程时,我习惯于在我添加的每一个功能位后定期“运行”我的代码,只是为了检查它是否正常工作。

我想用Ruby做到这一点,但我有一点点困难。

我有以下的类,我将使用作为我的程序菜单,所以,我希望用户,当他们运行程序开始:

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 

然而,当我运行类,控制台显示一条线,说明它正在运行它,但接着下一行是“处理完成退出码0”

我不知道这是为什么?在Java中,有一个主要的方法,程序总是会去指导它在运行时该怎么做......但就我可以告诉Ruby而言,不需要主要方法?如果是这种情况,我该如何运行我迄今为止所写的内容来检查我是否正确编写了它?

* 修订 **

好吧,我在该行

Application.new 

的建议补充,这就是brilliant-菜单现在打印出来。我从菜单中选择了选项1,并且该行在控制台中打印出来:

#<Module:0x1bd2c90>What would you like to do? 

然后再次打印出菜单。

选项1应该转到我模块类,它看起来像这样:

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 

一旦用户选择从主菜单中选择1,节目导航到模块级,我的预期它完全运行类,即显示提示用户,并且在任何他们在键盘上输入阅读,然后再返回到菜单,由线

navigateTo Application 

在我的最后指示“ addModule'功能。但是,由于某种原因,它似乎不是导航到Module类,或者直接跳到它的末尾。任何人都可以指出我在这里做错了吗?

+0

原帖尽管原始解决方案更新为显示次要问题 – Someone2088 2012-08-14 15:20:18

回答

3

当你运行一个ruby文件时,它会从头到尾运行文件。在你的文件中,你只是定义了一个类,所以它会在ruby中创建该类,然后什么也不做,因为你没有告诉它实例化该类的一个实例。在文件的结尾,添加Application.new,这将创建一个类的实例,并且,看你的代码,将打印和接收输入

+0

Brilliant-添加了该行,现在它正在打印出菜单。非常感谢! – Someone2088 2012-08-14 14:58:24

0

您的代码可能是这样的:

class Application 

    def run 
    puts "What would you like to do? 
     1: Add module to a scheme 
     ...." 
    end 
    ... 
end 

Application.new.run 
# here the user will interact with you application 
相关问题