Project

General

Profile

Actions

Bug #1760

closed

Methods Expecting Paths Should Prefer #to_path Over #to_str

Added by runpaint (Run Paint Run Run) over 15 years ago. Updated over 13 years ago.

Status:
Closed
Target version:
ruby -v:
ruby 1.9.2dev (2009-07-08 trunk 23995) [i686-linux]
Backport:
[ruby-core:24257]

Description

=begin
Methods that expect paths usually call #to_path on their argument. They may also try #to_str. When an argument responds to both of these methods, I suggest that #to_path is preferred as it is the more specific, thus more likely to be correct.

Dir.chdir and File.open, at the least, prefer #to_str over #to_path.

$ cat /tmp/path.rb
p = Class.new do
def to_str; 'str' ; end
def to_path; 'path'; end
end

File.open(p.new)

$ ruby -v /tmp/path.rb
ruby 1.9.2dev (2009-07-08 trunk 23995) [i686-linux]
/tmp/path.rb:6:in initialize': No such file or directory - str (Errno::ENOENT) from /tmp/path.rb:6:in open'
from /tmp/path.rb:6:in `'
=end


Related issues 1 (0 open1 closed)

Has duplicate Backport191 - Backport #3604: File.path, File.open, etc call to_str twice on the path argumentClosedyugui (Yuki Sonoda)Actions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0