Issue #10011 has been updated by Leif Eriksen.


I agree with the confusion - initially I thought :to_path could be used, but despite its name it returns a string, hence the suggestion for :to_pathname, that allows things to implement a way to have themselves converted to a Pathname.

I was going to do it your way initially, but I decided on the slightly longer version because
1. I didn't want to call Pathname::new if we already had been passed a Pathname (or class that inherits from that) - I think preserving the original subclass is important
2. I wanted an nice exception, related to trying to create the Pathname, raise right at that point, to explain where the user had gone wrong.

What you have in your code is pretty much what _all_ the tests for relative_path_from do, which might have been taken as a sign on the original implementation, however that was > 8 years ago.

Unless you (or someone else) thinks my reasons are poor, I'll commit as is in 24 hours.

----------------------------------------
Bug #10011: Passing a string to Pathname#relative_path_from results in NoMethodError
https://bugs.ruby-lang.org/issues/10011#change-47691

* Author: Jack Nagel
* Status: Open
* Priority: Normal
* Assignee: 
* Category: 
* Target version: 
* ruby -v: ruby 2.2.0dev (2014-07-05 trunk 46706) [x86_64-darwin13]
* Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN
----------------------------------------
When a string is passed to Pathname#relative_path_from, a NoMethodError is raised.

```
irb(main):001:0> require "pathname"
=> true
irb(main):002:0> Pathname.new("/usr/bin/cc").relative_path_from Pathname.new("/usr/bin")
=> #<Pathname:cc>
irb(main):003:0> Pathname.new("/usr/bin/cc").relative_path_from("/usr/bin")
NoMethodError: undefined method `cleanpath' for "/usr/bin":String
	from /Users/jacknagel/.rubies/ruby-2.2.0/lib/ruby/2.2.0/pathname.rb:493:in `relative_path_from'
	from (irb):3
	from /Users/jacknagel/.rubies/ruby-2.2.0/bin/irb:11:in `<main>'
```

I think either converting the argument to a Pathname or raising TypeError would be acceptable here.



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