在 ruby 中使用 parseconfig 导入配置
Importing config with parseconfig in ruby
我有一个 .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}"
思路来自那个独库: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)
也许有人可以帮忙?
除了我修复的一些代码故障外,我执行代码没有问题:
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]
你的问题是诱发的
我有一个 .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}"
思路来自那个独库: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)
也许有人可以帮忙?
除了我修复的一些代码故障外,我执行代码没有问题:
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]
你的问题是诱发的