Bug #3604: File.path, File.open, etc call to_str twice on the path argument
http://redmine.ruby-lang.org/issues/show/3604

Author: Tomas Matousek
Status: Open, Priority: Normal
ruby -v: ruby 1.9.1p378 (2010-01-10 revision 26273) [i386-mingw32]

class C
  def respond_to?(name)
    puts "C: #{name}"
    return false
  end
end

File.path(C.new) rescue p $!
File.open(C.new) rescue p $!
File.ctime(C.new) rescue p $!
File.dirname(C.new) rescue p $!

prints:

C: to_str
C: to_path
C: to_str
#<TypeError: can't convert C into String>
C: to_int
C: to_hash
C: to_str
C: to_path
C: to_str
#<TypeError: can't convert C into String>
C: to_io
C: to_str
C: to_path
C: to_str
#<TypeError: can't convert C into String>
C: to_str
C: to_path
C: to_str
#<TypeError: can't convert C into String>

Is there any reason why to_str is called twice on the arguments? Shouldn't to_path be called first and then to_str (i.e. the path conversion should be preferred over string conversion)?


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