File Rename in Ruby
This is my first post, and I hope not the last.
While doing some exercise of the book Learn to Program of Chris Pine, trying to rename some files as part of some exercise, that was renaming a bunch of picture from a folder, I noticed a strange functionality.
Using the method
File.rename(old_name, new_name) and after running my program it seems that the file haven’t been rename instead it has been deleted, but that’s not what happen.
What ruby was doing is renaming and moving the file to my printing working directory.
What really solve it was giving an absolute path to the method.
And I thought that was some tedious work, I look up in Ruby doc and found that
ruby is smart enough to provide with a method.
File.join(Dir.pwd, "new_name") this method will concatenate every string we pass it, providing the path to our file.
This is my final code for the exercise.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24