[英]Ruby program- method call doesn't appear to be working
I am writing a program using Ruby on Rails to allow users to manage University courses, their modules, and the students enrolled on them. 我正在使用Ruby on Rails编写一个程序,以允许用户管理大学课程,他们的模块以及注册的学生。
Currently, I have two classes: Application.rb and CourseModules.rb: 当前,我有两个类:Application.rb和CourseModules.rb:
Application.rb is the class that I am using to interface with the user- it currently looks like this: Application.rb是我用来与用户进行交互的类,它目前看起来像这样:
Code: 码:
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
and CourseModules.rb is where I am doing all of the work- it currently looks like this: 和CourseModules.rb是我正在进行的所有工作-当前看起来像这样:
Code: 码:
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
At present, when I run the program from Application.rb, I am presented with the menu, and I choose option 1: "Add module to a scheme" 目前,当我从Application.rb运行程序时,将显示菜单,并选择选项1:“将模块添加到方案”
I am then asked to enter a scheme name, which I do, and a line appears telling me that the scheme has been added to the system. 然后,我被要求输入一个方案名称,然后,出现一行,告诉我该方案已添加到系统中。
Next, I am asked to enter the name of the module, which I do, and am then asked if I'd like to enter another module. 接下来,要求我输入模块的名称,然后输入是否要输入另一个模块。 If I type 'y' or 'yes', I am allowed to enter the name of another module. 如果输入“ y”或“ yes”,则允许输入另一个模块的名称。 However, if I type 'n' or anything other than 'y' or 'yes', the program crashes and I get a few errors in the console. 但是,如果我键入“ n”或“ y”或“ yes”以外的任何其他内容,程序将崩溃,并且控制台中会出现一些错误。
The first of these errors says: 这些错误中的第一个说:
in
add_module': undefined method
main_menu' for Application:Class (NoMethodError) 在add_module': undefined method
Application:Class的add_module': undefined method
(NoMethodError)
on the line: 在线上:
Application.main_menu
in the 在里面
def self.add_module
method in my CourseModules.rb class. 我的CourseModules.rb类中的方法。
What should happen is that when the user enters 'n' or anything other than 'y' or 'yes' when asked if they'd like to add another module, they should be taken back the program main menu. 应该发生的情况是,当用户询问是否要添加另一个模块时,如果输入“ n”或“ y”或“ yes”以外的其他值,则应将其带回程序主菜单。
Does anyone have any idea what I'm doing wrong here? 有人知道我在做什么错吗? How can I put it right? 我该如何纠正?
It looks like you are trying to call a class method with Application.main_menu when it is defined as an instance method on the Application class which is why you are getting an undefined_method. 看起来当您在Application类上将其定义为实例方法时,您正在尝试使用Application.main_menu调用类方法,这就是为什么要获取undefined_method的原因。 Try adding self to the main_menu method definition. 尝试将self添加到main_menu方法定义中。
class Application
def self.main_menu
....
end
end
Fix based on your comments: 根据您的评论进行修复:
You will need to modify the initialize method as well. 您还需要修改initialize方法。 You can do so by changing it to this 您可以通过将其更改为此
class Application
def initialize
self.class.main_menu
end
...
end
By adding just self before main_menu in the initialize method you are just telling initialize that you want to initialize a new method when you create a new Application class. 通过在initialize方法的main_menu前面添加self,您只是在告诉initialize您要在创建新的Application类时初始化一个新方法。 In order to tell initialize that you want this method to be a class method you need to pass in the class as well. 为了告诉Initialize您希望此方法成为类方法,您还需要传入该类。 I hope this makes sense. 我希望这是有道理的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.