在开发过程中运行Ruby程序

在开发过程中运行Ruby程序,ruby,Ruby,我第一次用Ruby编写程序(我使用RubyMine作为我的IDE),以前主要用Java编写 在用Java编程时,我习惯在添加了每一项功能后定期“运行”代码,只是为了检查它是否正常工作 我试着用Ruby做这个,但是我有点困难 我有下面的类,我将使用它作为程序的菜单,因此我希望用户在运行程序时从它开始: class Application # To change this template use File | Settings | File Templates. def initializ

我第一次用Ruby编写程序(我使用RubyMine作为我的IDE),以前主要用Java编写

在用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
但是,当我运行该类时,控制台会显示一行,说明它正在运行该类,但下一行是“Process finished with exit code 0”

我不明白为什么会这样?在Java中,有一个main方法,当程序运行时,程序总是在这个方法中执行指令。。。但据我所知,Ruby不需要main方法?如果是这种情况,我如何运行到目前为止我写的内容来检查我写的是否正确

*更新**

好的,我在这行加上了

Application.new
正如建议的那样,这太棒了——菜单现在打印出来了。我从菜单中选择了选项1,这一行在控制台中打印出来:

#<Module:0x1bd2c90>What would you like to do?
一旦用户从主菜单中选择了选项1,并且程序导航到模块类,我希望它完全运行该类,即向用户显示提示,并读入他们在键盘上键入的任何内容,然后导航回菜单,如行所示

navigateTo Application

在我的“addModule”函数的末尾。然而,出于某种原因,它似乎不是导航到模块类,就是直接跳到模块类的末尾。有人能指出我做错了什么吗?

当运行ruby文件时,它会从头到尾运行该文件。在您的文件中,您只是定义了一个类,所以它将在ruby中创建该类,然后什么也不做,因为您没有告诉它实例化该类的实例。在文件末尾,添加
应用程序.new
,它将创建该类的实例,并查看您的代码,打印和接收输入。您的代码可能如下所示:

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
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

精彩-添加了一行,现在正在打印菜单。非常感谢!更新原始帖子,以显示第二个问题,尽管有原始解决方案