RUBY:如何编写一个接受文件路径作为参数的方法来加载YAML文件

时间:2022-08-26 15:24:58

I have the method:

我有方法:

`# require modules here
 require "yaml"

def load_library(file_path) # code goes here file_path = YAML.load_file('emoticons.yml') file_path end

def load_library(file_path)#code goes here file_path = YAML.load_file('emoticons.yml')file_path end

And I am trying to get it to load the emoticons.yml file but when I try running the method with the file path argument

我试图让它加载emoticons.yml文件,但当我尝试使用文件路径参数运行该方法

load_library(".lib/emoticons.yml")

load_library( “LIB / emoticons.yml”)

I get the following error:

我收到以下错误:

<Errno::ENOENT: No such file or directory @ rb_sysopen - emoticons.yml>

backtrace:

回溯:

./lib/translator.rb:6:in load_library' 
./spec/translator_spec.rb:4:in block (3 levels) in <top (required)>' 
./spec/translator_spec.rb:4:in block (2 levels) in <top (required)>' 
./spec/translator_spec.rb:4:in `block (2 levels) in <top (required)>'

Is there something wrong with the syntax or maybe I need to put the require "yaml" within the method?

语法有问题,或者我需要在方法中加入require“yaml”吗?

2 个解决方案

#1


1  

The issue you're running into is that you're expecting the File commands to use the application root directory as the current directory (pwd), but it's using ENV['PWD'].

您遇到的问题是您希望File命令将应用程序根目录用作当前目录(pwd),但它使用ENV ['PWD']。

For example, if your file is at app/helpers/yaml.rb, your call to Yaml.load_file will look for the file at app/helpers/lib/emoticons.yml.

例如,如果您的文件位于app / helpers / yaml.rb,则对Yaml.load_file的调用将在app / helpers / lib / emoticons.yml中查找该文件。

If the file is actually in another directory, like your_rails_app/lib/emoticons.yml, you can specify the directory (In Ruby, how do I specify a file in another directory as an input?).

如果文件实际上在另一个目录中,例如your_rails_app / lib / emoticons.yml,则可以指定目录(在Ruby中,如何将另一个目录中的文件指定为输入?)。

Rails also has an easier way of getting to this: Rails.root. This method returns a Pathname instance pointing to the directory of your Rails application. A convenient method on Pathname is join, which will automatically concatenate the strings you hand it and put the necessary forward slashes in between without duplicates.

Rails还有一种更简单的方法:Rails.root。此方法返回指向Rails应用程序目录的Pathname实例。 Pathname上一个方便的方法是join,它会自动连接你交给它的字符串,并在两者之间放置必要的正斜杠而不重复。

Thus, you can use this to specify the full path of your file for Yaml.load_file:

因此,您可以使用它来为Yaml.load_file指定文件的完整路径:

YAML.load_file(Rails.root.join('lib', 'emoticons.yml'))

If you want to handle that in the method you can, or you can specify the full path in the argument you send to the method.

如果要在方法中处理,或者可以在发送给方法的参数中指定完整路径。

#2


0  

User abosulte path instead of relative path

用户abosulte路径而不是相对路径

Instead of

代替

.lib/emoticons.yml

的.lib / emoticons.yml

try using

尝试使用

Rails.root.join('lib', 'emoticons.yml')

Rails.root.join('lib','emoticons.yml')

#1


1  

The issue you're running into is that you're expecting the File commands to use the application root directory as the current directory (pwd), but it's using ENV['PWD'].

您遇到的问题是您希望File命令将应用程序根目录用作当前目录(pwd),但它使用ENV ['PWD']。

For example, if your file is at app/helpers/yaml.rb, your call to Yaml.load_file will look for the file at app/helpers/lib/emoticons.yml.

例如,如果您的文件位于app / helpers / yaml.rb,则对Yaml.load_file的调用将在app / helpers / lib / emoticons.yml中查找该文件。

If the file is actually in another directory, like your_rails_app/lib/emoticons.yml, you can specify the directory (In Ruby, how do I specify a file in another directory as an input?).

如果文件实际上在另一个目录中,例如your_rails_app / lib / emoticons.yml,则可以指定目录(在Ruby中,如何将另一个目录中的文件指定为输入?)。

Rails also has an easier way of getting to this: Rails.root. This method returns a Pathname instance pointing to the directory of your Rails application. A convenient method on Pathname is join, which will automatically concatenate the strings you hand it and put the necessary forward slashes in between without duplicates.

Rails还有一种更简单的方法:Rails.root。此方法返回指向Rails应用程序目录的Pathname实例。 Pathname上一个方便的方法是join,它会自动连接你交给它的字符串,并在两者之间放置必要的正斜杠而不重复。

Thus, you can use this to specify the full path of your file for Yaml.load_file:

因此,您可以使用它来为Yaml.load_file指定文件的完整路径:

YAML.load_file(Rails.root.join('lib', 'emoticons.yml'))

If you want to handle that in the method you can, or you can specify the full path in the argument you send to the method.

如果要在方法中处理,或者可以在发送给方法的参数中指定完整路径。

#2


0  

User abosulte path instead of relative path

用户abosulte路径而不是相对路径

Instead of

代替

.lib/emoticons.yml

的.lib / emoticons.yml

try using

尝试使用

Rails.root.join('lib', 'emoticons.yml')

Rails.root.join('lib','emoticons.yml')