0

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

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.

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.

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 Answers2

1

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

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:

file_path = File.expand_path(File.dirname(__FILE__))
absolute_path = File.join(file_path, file_name)
File.write(absolute_path, new_user.to_yaml)
Martin Konecny
  • 57,827
  • 19
  • 139
  • 159
  • so, if I wanted to write it into a folder that was in the folder containing the Ruby script, and the folder was called library, how would I put it there? and how would I read it from there? –  May 13 '15 at 11:40
  • You would use the last example. Put that code into the file in your library folder, and it will write there. – Martin Konecny May 13 '15 at 14:59
  • alright, in that last example, what is the (__FILE__) bit? –  May 14 '15 at 00:21
  • okay. Does the location specified by file_path go in quotes or not? –  May 14 '15 at 11:52
  • Not sure I understand. `file_path` is a string, so you don't need to quote it anymore. – Martin Konecny May 14 '15 at 16:30
  • okay. I'm sorry. I misread it. Where do I actually specify the location to save it to? –  May 14 '15 at 23:31
0

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

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

matt
  • 515,959
  • 87
  • 875
  • 1,141