2012-08-14 91 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 
相關問題