tags:

views:

91

answers:

6

What are the benefits of using FileUtils methods http://ruby-doc.org/core/classes/FileUtils.html than the equivalent Bash commands?

+2  A: 

They are easier to call from inside Ruby scripts because they accept Ruby objects as arguments. This means that you don't have to handle escaping and what not every time you call them.

Douglas
You don't have to worry about escaping when doing `system("cp", file1, file2)` either (though you do when you do `system("cp #{file} #{file2}")`).
sepp2k
What do you mean by escaping? What has escaping to with system("cp source_file destination_file")
never_had_a_name
@ajsie: You need to escape spaces and special shell character when using the one-argument version of `system`. E.g. `system("cp source file destination file")` will not work, because the spaces are not escaped. However `system("cp", "source file", "destination file")` will work fine.
sepp2k
what about backtick? ``cp source dest``
Matt Briggs
+4  A: 

The FileUtils methods work on Windows.

sepp2k
so can shell tools. (ie cygwin, GNU win32)
ghostdog74
@ghostdog74: Only if you install them first. Point is: if you use FileUtils your script will run on every system with ruby. If you use cp your script will run on every system with ruby and cp. There's no reason to add an additional dependency for something as simple as copying files.
sepp2k
you have to install Ruby first as well.
ghostdog74
@ghostdog74: Well, of course, that's why I said "*additional* dependency". You can do nothing about the dependency to ruby (other than using rubyscript2exe to to bundle ruby with your script), but you can easily remove the dependency to coreutils, so I don't see why you wouldn't.
sepp2k
+1  A: 

when you farm stuff out to the shell, you are adding a dependency on those apps. FileUtils is pure ruby, so it works (and works the same, more or less) anywhere that ruby works.

Matt Briggs
A: 

I would not say there is no benefits in using Ruby's FileUtils, since you can use them anywhere you have Ruby( especially if your task is in web development). But that does not mean you can't use those shell tools in other platforms as well. Yes, you can write your scripts in *nix shell, and you can run them as well with little or no modification in, say, Windows using cygwin or GNU win32.(and others). In terms of benefits of Ruby's FileUtils against shell's, its only minimal, since what you can do with FileUtils you can do also with shell's.

ghostdog74
+2  A: 

Over and above the fact that you don't have to worry about ensuring your target platform has the specific tools you're using installed, and over and above the problem of doing proper quoting of shell oddities (especially problematical if you target both Windows and Unix-alikes -- Cygwin, GNUWin32, etc. notwithstanding), if you use Ruby's FileUtils you have the moderately-sized overhead of a Ruby function call while if you use external utilities you have the rather sizable overhead of firing up an external process each and every "call".

JUST MY correct OPINION
but you have to worry about whether Ruby is installed on the target as well.
ghostdog74
It should be painfully obvious, given that the person asking the question is talking about Ruby modules, that the program is undoubtedly a Ruby program.
JUST MY correct OPINION
i am not saying its not a Ruby program. I am talking your statement "Over and above the fact that you don't have to worry about ensuring your target platform has the specific tools you're using installed". Obviously, you will also need to ensure the target platform has Ruby before he can run his Ruby program.
ghostdog74
From context it should be obvious that "ensuring the target platform has the specific tools you're using installed **beyond the <profanity/>ing programming language you're implementing your <profanity/>ing program in** " is what is meant. And indeed I doubt anybody who's not being contrary for the sake of being contrary alone read it as anything but that.
JUST MY correct OPINION
+1  A: 
  • Works across multiple platforms
  • Does not spawn a new process to issue the command (so it consumes less resources)
Manfred Stienstra
you have to execute ruby interpreter to run the ruby script. This is spawning a process. isn't it not?
ghostdog74
Ah, I think I've misinterpreted the question. I thought he was asking about using the shell command from Ruby.
Manfred Stienstra