2015-04-24 40 views
0

我有与内容的.publicancreators.cfg:导入配置与parseconfig在红宝石

# Global 
name = Sascha Manns 
email_private = [email protected] 

然后,我已经定义了一个方法:

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}" 

的想法来自于数独: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) 

也许任何人都可以帮助吗?

+0

_Sidenote__通过返回'返回%w(姓名电子邮件)'您返回_strings_''名称''和'“电子邮件”',而不是它们的值。 'return'关键字也是多余的。最后一行应该是'[name,email_private]'。错误是诱发的,它与解析配置无关。 – mudasobwa

+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 [[email protected]] 

你的问题引起。

+0

非常感谢。这有助于:-) –