Ruby在print语句中使用变量

时间:2012-08-21 09:07:49

标签: 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 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

修改

好的,我已经提出了更改,但是,当我现在运行我的程序时,我收到一条错误,说“课程模块的未定义方法'add_module':Class(NoMethodError)。

尽管我从类中删除了我的旧方法(称为addModuleToScheme),然后编写了新方法,更新了菜单中的方法调用,并保存了这两个类。看起来我的菜单仍然试图调用旧方法,即使它不再存在......任何想法?

1 个答案:

答案 0 :(得分:0)

您的代码存在很多问题 -

  1. 在方法名称和变量名称中使用Un-Ruby样式语法。
  2. 使用=begin; comment; =end即使单行评论也不是很好。我个人更喜欢使用#style甚至多行评论。
  3. 可互换使用本地和实例变量。例如,在最后一行中,您搜索哈希键以查找局部变量schemeName,并在随后的puts中使用实例变量@schemeName,但我怀疑您可能正在尝试实现插值你需要使用"#{schemeName}"代替"@schemeName"
  4. 的效果

    然而,除非我正确理解您想要实现的目标(即创建方案的哈希,方案为关键,模块为值),请尝试以下代码。在这个例子中,我假设你想在方案和模块之间建立1:1的关系,但是如果你想为每个方案定义多个模块,你可以通过添加另一个循环并用一个数组替换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