[ruby-core:82830] [Ruby trunk Bug#13908] Result of Pathname#dirname / File.dirname handling of paths with trailing File::SEPARATOR is surprising

From: rocifier@...
Date: 2017-09-16 09:13:31 UTC
List: ruby-core #82830
Issue #13908 has been updated by rocifier (Ryan O'Connor).


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?

----------------------------------------
Bug #13908: Result of Pathname#dirname / File.dirname handling of paths with trailing File::SEPARATOR is surprising
https://bugs.ruby-lang.org/issues/13908#change-66713

* Author: davemyron (Dave Myron)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: 2.4.1p111
* Backport: 2.2: UNKNOWN, 2.3: UNKNOWN, 2.4: UNKNOWN
----------------------------------------
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? 



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>

In This Thread

Prev Next