Project

General

Profile

Actions

Feature #7361

closed

Adding Pathname#touch

Added by aef (Alexander E. Fischer) about 12 years ago. Updated over 8 years ago.

Status:
Rejected
Target version:
-
[ruby-core:49374]

Description

Pathname has an #mkdir method to create a directory at the path, but does not have a #touch method to create an empty file at that path.

There were numerous cases where I had to fallback to using

new_file = Pathname.new('location/for/new/file')
FileUtils.touch(new_file)

instead of simply being able to use:

new_file.touch

I would like to add this method. If you like it, let me know. I will provide a patch then.


Related issues 2 (0 open2 closed)

Related to Ruby master - Feature #7378: Adding Pathname#writeClosedakr (Akira Tanaka)06/02/2012Actions
Related to Ruby master - Feature #17295: Feature: Create a directory and file with Pathname#touchRejectedakr (Akira Tanaka)Actions

Updated by mame (Yusuke Endoh) about 12 years ago

  • Status changed from Open to Assigned
  • Assignee set to akr (Akira Tanaka)
  • Target version set to 2.6

Updated by Salzig (Ben Rexin) almost 10 years ago

Any way i can help?

Updated by djberg96 (Daniel Berger) almost 10 years ago

I'm a little surprise there isn't an option for this in FileUtils. Something like FileUtils.touch('/location/for/new', :force => true). I'd be in favor of adding that, or an option to FileUtils.mkpath.

Updated by dsisnero (Dominic Sisneros) over 8 years ago

Any update on this

class Pathname

  def touch(options={})
    FileUtils.touch(@path, options)
  end

end

Pathname('test.pdf').touch(mtime: 1.hour.ago)

"#mkpath" almost works but doesn't have the mtime option

Updated by akr (Akira Tanaka) over 8 years ago

  • Status changed from Assigned to Rejected

I don't like the method name "touch".

"touch" command has multiple features:
create empty file, update mtime.
So, the intent of touch method is ambiguous.

Updated by najamelan (Naja Melan) over 8 years ago

I prefer something like (warning, untested code):

def touch( subPath = '', **options )

	path = @path
	ret  = self

	if directory?

		ret  = join subPath
		path = ret.to_path

	end

	FileUtils.touch( path, **options )

	ret

end

lets you do:

mydir.touch 'somefile'

I think touch is a good name because everybody already knows it and it's what people will try out spontaneously before even looking in the docs why it doesn't work. Joel Spolsky explains quite well why this is a good thing.

Updated by shyouhei (Shyouhei Urabe) over 8 years ago

We don't always blindly import broken designs; for instance Ruby do not have creat(2) equivalent method. "Everybody knows it" is (definitely a good property but) not enough.

When it comes to touch, there already is FileUtils.touch so your urgent needs are already satisfied I believe. Here on Pathname let us consider a clearer design.

Actions #8

Updated by mame (Yusuke Endoh) 3 months ago

  • Related to Feature #17295: Feature: Create a directory and file with Pathname#touch added
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0