Ruby程序方法调用似乎不起作用

时间:2012-08-22 11:09:11

标签: ruby-on-rails

我正在使用Ruby on Rails编写一个程序,允许用户管理大学课程,他们的模块以及注册的学生。

目前,我有两个类:Application.rb和CourseModules.rb:

Application.rb是我用来与用户交互的类 - 它目前看起来像这样:

代码:

class Application
# To change this template use File | Settings | File Templates.
require './courseModules.rb'
def initialize
  main_menu
end

=begin
  def navigateTo(what)
  what.new(v).display
  mainMenu
  end
=end

def main_menu
  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.add_module
    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

和CourseModules.rb是我正在完成所有工作的地方 - 目前看起来像这样:

代码:

class CourseModules
# To change this template use File | Settings | File Templates.
   @@moduleScheme = nil
   @@moduleYear = nil
   #@moduleTitle = ""
   @noOfModulesInScheme = 0

   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


# Attempt at add_module method on 21/08/2012 at 16:30
def self.add_module
  schemes = {}
  scheme_exists = false
  add_another_scheme = true
  add_another_module = true

  while add_another_scheme
    print "Enter scheme name: "
    scheme_name = gets
    schemes.has_key?(scheme_name.chop) ? scheme_exists = true : scheme_exists = false

   if !scheme_exists
     schemes[scheme_name.chop] = []
     puts "Scheme #{scheme_name.chop} has been added to the system"
   else
     scheme_exists = false
     puts "This scheme has already been added"
   end

   while add_another_module
     print "Enter module name: "
     module_name = gets
     schemes[scheme_name.chop].include?(module_name.chop) ? true : schemes[scheme_name.chop] << module_name.chop
     print "Add another module? "
     ask_if_user_wants_to_add_another_module = gets
     if(ask_if_user_wants_to_add_another_module.chop == "y" or ask_if_user_wants_to_add_another_module == "yes")
      add_another_scheme = false
     else if(ask_if_user_wants_to_add_another_module.chop != "y" or ask_if_user_wants_to_add_another_module != "yes")
       Application.main_menu
          end
   end

 end

 print "Add another scheme? "
 ask_if_user_wants_to_add_another_scheme = gets
 if !(ask_if_user_wants_to_add_another_scheme.chop == "y" or ask_if_user_wants_to_add_another_scheme.chop == "yes")
   add_another_scheme = false
 end
 puts @schemes

end

 while add_another_module
   print "Enter scheme name: "
   scheme_name = gets
   schemes.has_key?(scheme_name.chop) ? scheme_exists = true : scheme_exists = false

   if !scheme_exists
     print "Enter module name: "
     module_name = gets
     schemes[scheme_name.chop] = module_name.chop
     puts "Scheme #{scheme_name.chop} with module #{module_name} has been added to the system"
   else
     scheme_exists = false
     puts "This scheme has already been added"
     puts "Enter module name: "
     module_name = gets
     schemes[scheme_name.chop] = module_name.chop
     puts "Scheme #{scheme_name.chop} with module #{module_name} has been added to the system"
   end

   print "Add another module? "
   ask_if_user_wants_to_add_another_module = gets
   if !(ask_if_user_wants_to_add_another_module.chop == "y" or ask_if_user_wants_to_add_another_module.chop == "yes")
     add_another_module = false
   end
 end
 puts schemes
end

end

目前,当我从Application.rb运行程序时,我看到了菜单,我选择了选项1:“将模块添加到方案中”

然后我被要求输入一个方案名称,我这样做,然后会出现一条线告诉我该方案已被添加到系统中。

接下来,我被要求输入模块的名称,然后询问我是否要输入另一个模块。如果我输入'y'或'yes',我可以输入另一个模块的名称。 但是,如果我输入“n”或“y”或“是”以外的任何内容,程序会崩溃,我在控制台中会出现一些错误。

第一个错误是:

  应用程序:Class(NoMethodError)的add_module': undefined method main_menu'中的

就行:

Application.main_menu

中的

def self.add_module
我的CourseModules.rb类中的

方法。

当用户输入“n”或“y”或“是”以外的任何内容时,如果他们想要添加另一个模块,应该将其带回程序主菜单。

有谁知道我在这里做错了什么?我怎么能说得对呢?

1 个答案:

答案 0 :(得分:1)

当它被定义为Application类的实例方法时,看起来你正试图用Application.main_menu调用类方法,这就是为什么你得到一个undefined_method。尝试将self添加到main_menu方法定义中。

class Application
  def self.main_menu
    ....
  end
end

根据您的评论进行修复:

您还需要修改initialize方法。您可以将其更改为此

class Application
  def initialize
    self.class.main_menu
  end

  ...

end

通过在initialize方法中添加main_menu之前的self,您只是在创建新的Application类时告诉初始化您要初始化新方法。为了告诉初始化你希望这个方法是一个类方法,你也需要传入类。我希望这是有道理的。