Project

General

Profile

Actions

Bug #13908

closed

Result of Pathname#dirname / File.dirname handling of paths with trailing File::SEPARATOR is surprising

Added by davemyron (Dave Myron) about 7 years ago. Updated about 7 years ago.

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

Description

Given File.dirname("/some/folder/") the result of /some is very surprising because the path is "obviously" a directory without a file specified. (I got bit by this today… yes, I needed more tests)

My expectation was that it would return /some/folder. My expectation seems consistent with the method's documentation which states "Returns all components of the filename given in file_name except the last one." – the "last one" should be the item after the last File::SEPARATOR ("/" on my system)

What is the reasoning for the above being the behavior?

Actions #1

Updated by davemyron (Dave Myron) about 7 years ago

  • Subject changed from Result of Pathname#dirname / File.dirname handling of paths with trailing slashes is surprising to Result of Pathname#dirname / File.dirname handling of paths with trailing File::SEPARATOR is surprising
Actions #2

Updated by davemyron (Dave Myron) about 7 years ago

  • Description updated (diff)
Actions #3

Updated by davemyron (Dave Myron) about 7 years ago

  • Description updated (diff)

Updated by rocifier (Ryan O'Connor) about 7 years ago

davemyron (Dave Myron) wrote:

Given File.dirname("/some/folder/") the result of /some is very surprising because the path is "obviously" a directory without a file specified. (I got bit by this today… yes, I needed more tests)

My expectation was that it would return /some/folder. My expectation seems consistent with the method's documentation which states "Returns all components of the filename given in file_name except the last one." – the "last one" should be the item after the last File::SEPARATOR ("/" on my system)

What is the reasoning for the above being the behavior?

The path is obvious a directory.. true. However, the docs says to call the method with a filename, not a directory name. I'm not quite sure what you would be trying to achieve by calling it with just a directory name. Perhaps you intend to use Pathname.new("/some/folder/") instead?

Actions #5

Updated by nobu (Nobuyoshi Nakada) about 7 years ago

  • Status changed from Open to Closed

Applied in changeset trunk|r59933.


file.c: [DOC] separators at dirname and basename

  • file.c (rb_file_s_basename, rb_file_s_dirname): [DOC] state
    that trailing separators will be stripped first, like as
    basename(1) and dirname(1). [ruby-core:82828] [Bug #13908]
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0