Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Ruby存储和调用YAML文件中的数据?_Ruby_Cucumber_Capybara_Yaml - Fatal编程技术网

如何使用Ruby存储和调用YAML文件中的数据?

如何使用Ruby存储和调用YAML文件中的数据?,ruby,cucumber,capybara,yaml,Ruby,Cucumber,Capybara,Yaml,我正在使用Cucumber和Capybara编写特性和步骤定义,我希望将用户凭据存储在YAML文件中 我的问题是,如果我的support/config.yml中有一个cred.yml文件,并且我在env.rb中加载该文件(config=YAML.load_文件(“/config/config.yml”)),那么所有信息都可以访问吗?如果是,我将如何从env_1访问/调用user1 或者,如果我希望一次只加载一个/多个select环境,我将如何做到这一点?我如何访问/呼叫不同的用户 大概是这样的:

我正在使用Cucumber和Capybara编写特性和步骤定义,我希望将用户凭据存储在YAML文件中

我的问题是,如果我的support/config.yml中有一个cred.yml文件,并且我在env.rb中加载该文件(
config=YAML.load_文件(“/config/config.yml”)
),那么所有信息都可以访问吗?如果是,我将如何从env_1访问/调用user1

或者,如果我希望一次只加载一个/多个select环境,我将如何做到这一点?我如何访问/呼叫不同的用户

大概是这样的:
CONFIG=YAML.load_文件(“/CONFIG/CONFIG.yml”)[ENV]

config.yml文件内容:

env_1:

 `user1: admin`
 `password1: password`
 `user2: teacher`
 `password2: password`
env_2:

 `user: student`
 `password: password`
 `user2: assistant`
 `password2: password`

YAML::load_file
返回嵌套哈希:

require 'yaml'
config = YAML.load_file("config.yml") #=> {"env_1"=>{"user1"=>"admin", "password1"=>"password", "user2"=>"teacher", "password2"=>"password"}, "env_2"=>{"user"=>"student", "password"=>"password", "user2"=>"assistant", "password2"=>"password"}}
您可以通过以下方式访问
env_1

config["env_1"] #=> {"user1"=>"admin", "password1"=>"password", "user2"=>"teacher", "password2"=>"password"}
其价值观包括:

config["env_1"]["user1"] #=> "admin"
config["env_1"]["user2"] #=> "teacher"
访问
env_2
会相应地工作:

config["env_2"]["user"]  #=> "student"
假设您的
config.yml
如下所示:

env_1:
  user1: admin
  password1: password
  user2: teacher
  password2: password
env_2:
  user: student
  password: password
  user2: assistant
  password2: password

YAML::load_file
返回嵌套哈希:

require 'yaml'
config = YAML.load_file("config.yml") #=> {"env_1"=>{"user1"=>"admin", "password1"=>"password", "user2"=>"teacher", "password2"=>"password"}, "env_2"=>{"user"=>"student", "password"=>"password", "user2"=>"assistant", "password2"=>"password"}}
您可以通过以下方式访问
env_1

config["env_1"] #=> {"user1"=>"admin", "password1"=>"password", "user2"=>"teacher", "password2"=>"password"}
其价值观包括:

config["env_1"]["user1"] #=> "admin"
config["env_1"]["user2"] #=> "teacher"
访问
env_2
会相应地工作:

config["env_2"]["user"]  #=> "student"
假设您的
config.yml
如下所示:

env_1:
  user1: admin
  password1: password
  user2: teacher
  password2: password
env_2:
  user: student
  password: password
  user2: assistant
  password2: password

为什么不试试看会发生什么?为什么不试试看会发生什么?只是为了添加,如果您使用的是Rails,那么创建具有不同访问权限的配置可能会有所帮助,即
config=YAML.load(“config.yml”)。如果您使用的是Rails,那么使用“不相关的访问权限”
只是为了添加,使用不同的访问权限创建配置可能会有所帮助,即使用不同的访问权限创建配置,即
config=YAML.load(“config.yml”)