Ruby on rails Ruby在print语句中使用变量

Ruby on rails Ruby在print语句中使用变量,ruby-on-rails,ruby,Ruby On Rails,Ruby,我有以下代码,通过从程序菜单中选择选项1调用: def self.addModuleToScheme =begin Create an empty hash for the schemes =end schemes = Hash.new() =begin Allow user to enter scheme names into a set of variables, and use each scheme name as a hash/ array of modules. Then allo

我有以下代码,通过从程序菜单中选择选项1调用:

def self.addModuleToScheme
=begin
Create an empty hash for the schemes
=end
 schemes = Hash.new()
=begin
Allow user to enter scheme names into a set of variables, and use each scheme name as a hash/ array of modules.
Then allow the user to enter the the modules for each scheme into each of the hashes

Create specific hash elements by using the following line:
schemes = {:scheme1 => scheme1variable, :scheme2 => scheme2variable}
=end

 puts "What is the name of the scheme that you would like to add a module to? "
 schemeName = gets
=begin
Need to use an if statement here to check whether or not the scheme already exists, if it doesn't, create it, if it does,
tell the user that it does.
=end
 if schemes.has_key?(schemeName)
   puts "This scheme has already been added "
 else
   schemes[@noOfModulesInScheme] = schemeName
 end

 @noOfModulesInScheme + 1
# moduleName.moduleScheme = schemeName

# Print to check that scheme has been added to system:
 if schemes.has_key?(schemeName)
   puts "@schemeName has been added to the system "
 end
当前,当我运行代码时,将显示菜单,并选择选项1,即调用此代码时。然后我被问到我想添加模块的方案的名称是什么。我输入,然后程序退出

我想做的是,有一份打印件告诉我,我刚刚输入的方案已经添加到系统中,然后让我的程序继续问我想添加到方案中的模块

但是我不知道如何在一个句子中打印散列元素的内容。谁能给我指出正确的方向吗

编辑

如果有帮助的话,这是我程序中的另一个类,菜单是从中打印出来的

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

=begin
  def navigateTo(what)
  what.new(v).display
  mainMenu
  end
=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"
      CourseModules.addModuleToScheme
    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的未定义方法“add_module”:Class NoMethodError


尽管我从类中删除了调用addModuleToScheme的旧方法,然后编写了新方法,更新了菜单中的方法调用,并保存了这两个类,但仍然存在这种情况。似乎我的菜单仍在尝试调用旧方法,即使它不再存在。。。有什么想法吗?

您的代码有很多问题-

在方法名和变量名中使用非Ruby风格的语法。 用法=开始;评论=即使是单行评论也不好结束。就个人而言,我更喜欢使用风格,即使是多行评论。 可以互换使用局部变量和实例变量。例如,在最后一行中搜索局部变量schemeName的哈希键,在随后的puts中使用实例variable@schemeName,尽管我怀疑您可能试图实现插值效果,顺便说一句,您需要使用{schemeName}而不是@schemeName 但是,如果我正确理解了您想要实现的目标,即创建一个以scheme为键、module为值的方案散列,那么请尝试以下代码。在本例中,我假设您希望scheme和module之间具有1:1的关系,但是如果您希望为每个scheme定义多个模块,则可以通过添加另一个循环并在下面的代码中用数组替换scheme的字符串值来轻松实现

def add_module
    schemes = {}
    scheme_exists = false
    add_another_scheme = 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
                    print "Enter Module Name: "
                    module_name = gets
                    schemes[scheme_name.chop] = module_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

            print "Enter another scheme? "
            ask_user_if_would_like_to_add_another_scheme = gets
            if !(ask_user_if_would_like_to_add_another_scheme.chop == "Y" or ask_user_if_would_like_to_add_another_scheme.chop == "Yes")
                    add_another_scheme = false
            end
    end
    puts schemes
end

嗨,谢谢你的回复。您在那里所做的听起来正是我想要的-我尝试过更新代码,还更新了菜单中的方法调用以反映新的方法名称-当用户选择选项1时,我现在有了CourseModules.add_模块。然而,我现在得到一个add_module NoMethodError的“undefined method”错误,尽管在我的CourseModules类中将该方法重命名为该方法,并将其保存。有什么建议吗?啊,我认为在CourseModules类中,您需要定义add_module作为类方法。将def add_模块更改为def self.add_模块或def CourseModules.add_模块,它应该可以工作