Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby's File class vs. Rails' FileUtils

Now about 2 weeks into learning Ruby and Rails, I've found myself using the File class a lot for things like File.join, File.open, etc. Then I bumped into a need for File.copy only to find out that no such method exists. A little more looking uncovered Rails' FileUtils class and now I'm a little confused.

There are differences, of course, but there are also what appear to be redundancies. Is one preferred over the other where there are differences? Why do both exist (is it solely to handle the omissions in the Ruby core class)?

I'd just like to get a feel for how these things work together or conflict so I know how to move forward.

Thanks.

like image 889
Rob Wilkerson Avatar asked Aug 28 '09 01:08

Rob Wilkerson


1 Answers

FileUtils is part of the Ruby Core API. It's not Rails specific. Also, it's a module, so you could mix in some of the abilities it has to offer into another class. Your best bet is to probably read the File RDoc, as well as the FileUtils RDoc. Hope this helps you out a bit. Cheers.

Edit:

Is one preferred over the other where there are differences?

I don't know if there is much consensus over which to use when the methods have the same ultimate end–result, such as File.makedirs vs. FileUtils.mkdir_p, but I often see the latter in other peoples code. I think you will almost always use the two (File & FileUtils) in conjunction.

like image 151
theIV Avatar answered Sep 20 '22 10:09

theIV