I need to “touch” a file from my ruby script. (This is a unix-based
command that updates the timestamp on a file, creating it, if it
doesn’t already exist.) I’m running on Windows. A collegue found a port
of “touch” and installed it and called it using “system”, but i would
rather see a solution that requires less installation.

I am currently using the following code:

class File
def self.touch(filename), ‘a’){|f| f.puts ’ '}

This implementation is good enough for my present needs (touching an
xml file), but it has obvious side effects that could be a problem in
other contexts.

I did find a touch command in Ruby in the ptools package at but it did not work.
(It zeroed the file it touched!)

Any suggestions for a better “touch” in Ruby?

Bret, 'a'){|f| f.puts ' '}

Any suggestions for a better “touch” in Ruby?

What’s wrong with FileUtils.touch() ?

Philip H. wrote:

What’s wrong with FileUtils.touch() ?

That looks good. Thanks.

In retrospect, i realize that i should have used fxri first.


This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs