Project

General

Profile

Actions

Feature #12194

closed

File.dirname optional parameter

Added by nobu (Nobuyoshi Nakada) almost 9 years ago. Updated almost 4 years ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:74448]

Description

As talked at the last developers' meeting, I propose an addition of an optional parameter to File.dirname.

Often I see the code, like

File.dirname(File.dirname(path)) # or
File.expand_path("../..", path)

which are not concise.

This proposal can make them as:

File.dirname(path, 2)

Patch

Updated by vo.x (Vit Ondruch) almost 9 years ago

  • Subject changed from File.dirname optonal parameter to File.dirname optional parameter

Updated by Eregon (Benoit Daloze) almost 9 years ago

Using Pathname would already make this much nicer:

Pathname(path).parent.parent

And less error prone than File.dirname if the path is not absolute.
Just my opinion, but I do not like this particular syntax.

Updated by shevegen (Robert A. Heiler) almost 9 years ago

I myself have found Pathname harder or less elegant to use than the direct File* or Dir* methods so I am in support of the suggestion by nobu.

So to Benoit, I understand that you may consider File.dirname(path, 2) not ideal either (a user may wonder what the 2 means), the alternative
suggestion to use Pathname is not ideal either and additionally requires one to require pathname for that toplevel Pathname() method and
subsequent use of pathname:

require 'pathname'
# require 'fileutils'; FileUtils.mkdir_p('/tmp/foobar/') # <-- this line just for testing purpose, disregard it
Pathname("/tmp/foobar/").parent.parent.to_s # => "/"

Being able to directly do so on File.dirname() itself is good, in my opinion.

Actions #4

Updated by nobu (Nobuyoshi Nakada) almost 4 years ago

  • Description updated (diff)
Actions #6

Updated by nobu (Nobuyoshi Nakada) almost 4 years ago

  • Status changed from Open to Closed

Applied in changeset git|82b6f89283042b670975f3f7986432647101984e.


File.dirname optional level

  • file.c (rb_file_dirname_n): chomp N level of base names.
    [Feature #12194]
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0