Bug #1760: Methods Expecting Paths Should Prefer #to_path Over #to_str
http://redmine.ruby-lang.org/issues/show/1760

Author: Run Paint Run Run
Status: Open, Priority: Normal
Category: core
ruby -v: ruby 1.9.2dev (2009-07-08 trunk 23995) [i686-linux]

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 `<main>'


----------------------------------------
http://redmine.ruby-lang.org