如何在Ruby中指定要写入和读取的文件位置?

时间:2021-11-15 05:10:25

So, I have a function that creates an object specifying user data. Then, using the Ruby YAML gem and some code, I put the object to a YAML file and save it. This saves the YAML file to the location where the Ruby script was run from. How can I tell it to save to a certain file directory? (A simplified version of) my code is this

所以,我有一个函数创建一个指定用户数据的对象。然后,使用Ruby YAML gem和一些代码,我将对象放到YAML文件中并保存。这会将YAML文件保存到运行Ruby脚本的位置。如何告诉它保存到某个文件目录? (简化版)我的代码就是这个

print "Please tell me your name:  "
$name=gets.chomp
$name.capitalize!
print "Please type in a four-digit PIN number:  "
$pin=gets.chomp

I also have a function that enforces that the pin be a four-digit integer, but that is not important.

我还有一个函数强制pin是一个四位整数,但这并不重要。

Then, I add this to an object

然后,我将它添加到一个对象

new_user=Hash.new (false)
new_user["name"]=$name
new_user["pin"]=$pin

and then add it to a YAML file and save it. If the YAML file doesn't exist, one is created. It creates it in the same file directory as the script is run in. Is there a way to change the save location? The script fo save the object to a YAML file is this.

然后将其添加到YAML文件并保存。如果YAML文件不存在,则创建一个。它在运行脚本的同一文件目录中创建它。有没有办法更改保存位置?将对象保存到YAML文件的脚本就是这样。

def put_to_yaml (new_user)
File.write("#{new_user["name"]}.yaml", new_user.to_yaml)
end
put_to_yaml(new_user)

Ultimately, the question is this: How can I change the save location of the file? And when I load it again, how can i tell it where to get the file from?

最终,问题是:如何更改文件的保存位置?当我再次加载它时,我怎么能告诉它从哪里获取文件?

Thanks for any help

谢谢你的帮助

2 个解决方案

#1


Currently when you use File.write it takes your current working directory, and appends the file name to that location. Try:

目前,当您使用File.write时,它会获取您当前的工作目录,并将文件名附加到该位置。尝试:

puts Dir.pwd #  Will print the location you ran ruby script from.

You can specify the absolute path if you want to write it in a specific location everytime:

如果要在每次特定位置将其写入,则可以指定绝对路径:

File.write("/home/chameleon/different_location/#{new_user["name"]}.yaml")

Or you can specify a relative path to your current working directory:

或者,您可以指定当前工作目录的相对路径:

# write one level above your current working directory
File.write("../#{new_user["name"]}.yaml", new_user.to_yaml)

You can also specify relative to your current executing ruby file:

您还可以指定相对于当前正在执行的ruby文件:

file_path = File.expand_path(File.dirname(__FILE__))
absolute_path = File.join(file_path, file_name)
File.write(absolute_path, new_user.to_yaml)

#2


You are supplying a partial pathname (a mere file name), so we read and write from the current directory. Thus you have two choices:

您正在提供部分路径名(仅仅是文件名),因此我们从当前目录中读取和写入。因此,您有两个选择:

  • Supply a full absolute pathname (personally, I like to use the Pathname class for this); or

    提供一个完整的绝对路径名(个人而言,我喜欢使用Pathname类);要么

  • Change the current directory first (with Dir.chdir)

    首先更改当前目录(使用Dir.chdir)

#1


Currently when you use File.write it takes your current working directory, and appends the file name to that location. Try:

目前,当您使用File.write时,它会获取您当前的工作目录,并将文件名附加到该位置。尝试:

puts Dir.pwd #  Will print the location you ran ruby script from.

You can specify the absolute path if you want to write it in a specific location everytime:

如果要在每次特定位置将其写入,则可以指定绝对路径:

File.write("/home/chameleon/different_location/#{new_user["name"]}.yaml")

Or you can specify a relative path to your current working directory:

或者,您可以指定当前工作目录的相对路径:

# write one level above your current working directory
File.write("../#{new_user["name"]}.yaml", new_user.to_yaml)

You can also specify relative to your current executing ruby file:

您还可以指定相对于当前正在执行的ruby文件:

file_path = File.expand_path(File.dirname(__FILE__))
absolute_path = File.join(file_path, file_name)
File.write(absolute_path, new_user.to_yaml)

#2


You are supplying a partial pathname (a mere file name), so we read and write from the current directory. Thus you have two choices:

您正在提供部分路径名(仅仅是文件名),因此我们从当前目录中读取和写入。因此,您有两个选择:

  • Supply a full absolute pathname (personally, I like to use the Pathname class for this); or

    提供一个完整的绝对路径名(个人而言,我喜欢使用Pathname类);要么

  • Change the current directory first (with Dir.chdir)

    首先更改当前目录(使用Dir.chdir)