在ruby中使用parseconfig导入配置

时间:2015-04-24 15:35:25

标签: ruby return-value config

我有一个包含该内容的.publicancreators.cfg:

# Global
name = Sascha Manns
email_private = Sascha.Manns@bdvb

然后我定义了一个方法:

def self.config
    config = ParseConfig.new(File.join(File.dirname(__FILE__), '.publicancreators.cfg'))
    name = config['name']
    email_private = config['email_private']
    return %w(name email)
end

使用我在主程序中运行的那个:

a, b = PublicanCreatorsGet.config
name = "#{a}"
email = "#{b}"

这个想法来自那个doku:https://mikeyhogarth.wordpress.com/2011/11/21/return-multiple-values-from-a-ruby-method/

如果我正在运行该代码,我会:

<class:PublicanCreators>': undefined local variable or method `config' for PublicanCreators:Class (NameError)

也许有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

除了我修复的一些代码故障,我执行代码没有问题:

require 'parseconfig'
class A
  def self.config
    config = ParseConfig.new('/tmp/q.cfg')
    name = config['name']
    email_private = config['email_private']
    [name, email_private]
  end 
end

a, b = A.config

puts "name is [#{a}], email is [#{b}]"
#⇒ name is [Sascha Manns], email is [Sascha.Manns@bdvb]

你的问题被诱发了。