Can I somehow use this
我可以以某种方式使用它
settings = {
'user1' => { 'path' => '/','days' => '5' },
'user2' => { 'path' => '/tmp/','days' => '3' }
}
in a external file as settings?
在外部文件中作为设置?
How can I include this into my script?
如何将其包含在我的脚本中?
3 个解决方案
#1
62
The most common way to store configuration data in Ruby is to use YAML:
在Ruby中存储配置数据的最常用方法是使用YAML:
settings.yml
settings.yml中
user1:
path: /
days: 5
user2:
path: /tmp/
days: 3
Then load it in your code like this:
然后将其加载到您的代码中,如下所示:
require 'yaml'
settings = YAML::load_file "settings.yml"
puts settings.inspect
You can create the YAML file using to_yaml
:
您可以使用to_yaml创建YAML文件:
File.open("settings.yml", "w") do |file|
file.write settings.to_yaml
end
That said, you can include straight Ruby code also, using load
:
也就是说,你也可以使用load包含直接的Ruby代码:
load "settings.rb"
However, you can't access local variables outside the file, so you would have to change your code to use an instance variable or a global variable:
但是,您无法访问文件外的局部变量,因此您必须更改代码以使用实例变量或全局变量:
settings.rb
settings.rb
SETTINGS = {
'user1' => { 'path' => '/','days' => '5' },
'user2' => { 'path' => '/tmp/','days' => '3' }
}
@settings = { 'foo' => 1, 'bar' => 2 }
Then load it thus:
然后加载它:
load "settings.rb"
puts SETTINGS.inspect
puts @settings.inspect
#2
6
you can also use Marshal
你也可以使用Marshal
settings = {
'user1' => { 'path' => '/','days' => '5' },
'user2' => { 'path' => '/tmp/','days' => '3' }
}
data=Marshal.dump(settings)
open('output', 'wb') { |f| f.puts data }
data=File.read("output")
p Marshal.load(data)
#3
2
A really simple one is to use eval.
一个非常简单的方法是使用eval。
config.txt
的config.txt
{
'user1' => { 'path' => '/','days' => '5' },
'user2' => { 'path' => '/tmp/','days' => '3' }
}
program.rb
program.rb
configuration = eval(File.read("./config.txt"))
puts configuration['user1']
#1
62
The most common way to store configuration data in Ruby is to use YAML:
在Ruby中存储配置数据的最常用方法是使用YAML:
settings.yml
settings.yml中
user1:
path: /
days: 5
user2:
path: /tmp/
days: 3
Then load it in your code like this:
然后将其加载到您的代码中,如下所示:
require 'yaml'
settings = YAML::load_file "settings.yml"
puts settings.inspect
You can create the YAML file using to_yaml
:
您可以使用to_yaml创建YAML文件:
File.open("settings.yml", "w") do |file|
file.write settings.to_yaml
end
That said, you can include straight Ruby code also, using load
:
也就是说,你也可以使用load包含直接的Ruby代码:
load "settings.rb"
However, you can't access local variables outside the file, so you would have to change your code to use an instance variable or a global variable:
但是,您无法访问文件外的局部变量,因此您必须更改代码以使用实例变量或全局变量:
settings.rb
settings.rb
SETTINGS = {
'user1' => { 'path' => '/','days' => '5' },
'user2' => { 'path' => '/tmp/','days' => '3' }
}
@settings = { 'foo' => 1, 'bar' => 2 }
Then load it thus:
然后加载它:
load "settings.rb"
puts SETTINGS.inspect
puts @settings.inspect
#2
6
you can also use Marshal
你也可以使用Marshal
settings = {
'user1' => { 'path' => '/','days' => '5' },
'user2' => { 'path' => '/tmp/','days' => '3' }
}
data=Marshal.dump(settings)
open('output', 'wb') { |f| f.puts data }
data=File.read("output")
p Marshal.load(data)
#3
2
A really simple one is to use eval.
一个非常简单的方法是使用eval。
config.txt
的config.txt
{
'user1' => { 'path' => '/','days' => '5' },
'user2' => { 'path' => '/tmp/','days' => '3' }
}
program.rb
program.rb
configuration = eval(File.read("./config.txt"))
puts configuration['user1']