简体   繁体   English

在gem中使用yaml文件

[英]Using yaml files within gems

I'm just working on my first gem (pretty new to ruby as well), entire code so far is here; 我正在研究我的第一个宝石(也是ruby的新手),到目前为止整个代码都在这里;

https://github.com/mikeyhogarth/tablecloth https://github.com/mikeyhogarth/tablecloth

One thing I've tried to do is to create a yaml file which the gem can access as a lookup (under lib/tablecloth/yaml/qty.yaml). 我试图做的一件事是创建一个yaml文件,gem可以作为查找访问(在lib / tablecloth / yaml / qty.yaml下)。 This all works great and the unit tests all pass, hwoever when I build and install the gem and try to run under irb (from my home folder) I am getting; 这一切都很好,单元测试全部通过,无论何时我构建并安装gem并尝试在irb(从我的主文件夹)下运行我得到;

Errno::ENOENT: No such file or directory - lib/tablecloth/yaml/qty.yaml

The code is now looking for the file in ~/lib/tablecloth... rather than in the directory the gem is installed to. 代码现在在〜/ lib / tablecloth中查找文件...而不是在gem安装到的目录中。 So my questions are; 所以我的问题是;

1) How should i change line 27 of recipe.rb such that it is looking in the folder that the gem is installed to? 1)我应该如何更改recipe.rb的第27行,以便它正在查找安装gem的文件夹?

2) Am I in fact approaching this whole thing incorrectly (is it even appropriate to use static yaml files within gems in this way)? 2)我实际上是不正确地接近这整个事情(以这种方式在gem中使用静态yaml文件是否合适)?

Well first of all you should refer to the File in the following way: 首先,您应该通过以下方式引用文件:

file_path = File.join(File.dirname(__FILE__),"yaml/qty.yaml")
units_hash = YAML.load_file(filepath)

File.dirname(__FILE__) gives you the directory in which the current file (recipe.rb) lies. File.dirname(__FILE__)为您提供当前文件(recipe.rb)所在的目录。 File.join connects filepaths in the right way. File.join以正确的方式连接文件路径。 So you should use this to reference the yaml-file relative to the recipe.rb folder. 因此,您应该使用它来引用对于recipe.rb文件夹的yaml文件。

If using a YAML-file in this case is a good idea, is something which is widely discussed. 如果在这种情况下使用YAML文件是一个好主意,那就是广泛讨论的内容。 I, myself think, this is an adequate way, especially in the beginning of developing with ruby. 我,我自己认为,这是一种适当的方式,特别是在开发红宝石时。

A valid alternative to yaml-files would be a rb-File (Ruby Code), in which you declare constants which contain your data. yaml文件的有效替代方法是rb-File(Ruby代码),您可以在其中声明包含数据的常量。 Later on you can use them directly. 稍后您可以直接使用它们。 This way only the ruby-interpreter has to work and you save computing time for other things. 这样只有ruby-interpreter才能工作,你可以节省其他事情的计算时间。 (no parser needed) (不需要解析器)

However in the normal scenario you should also take care that reading in a YAML file might fail. 但是,在正常情况下,您还应该注意读取YAML文件可能会失败。 So you should be able to handle that: 所以你应该能够处理:

file_path = File.join(File.dirname(__FILE__),"yaml/qty.yaml")
begin
  units_hash = YAML.load_file(filepath)
rescue Psych::SyntaxError
  $stderr.puts "Invalid yaml-file found, at #{file_path}"
  exit 1
rescue Errno::EACCES
  $stderr.puts "Couldn't access file due to permissions at #{file_path}"
  exit 1
rescue Errno::ENOENT
  $stderr.puts "Couldn't access non-existent file #{file_path}"
  exit 1
end

Or if you don't care about the details: 或者,如果您不关心细节:

file_path = File.join(File.dirname(__FILE__),"yaml/qty.yaml")
units_hash =     
  begin
    YAML.load_file(filepath)
  rescue Psych::SyntaxError, Errno::EACCES, Errno::ENOENT
    {}
  end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM