Issue #10011 has been updated by Jack Nagel.


I don't think we should add a method to String; there is an existing path coercion protocol (`to_path` and `File.path`) and even though String does not implement `to_path`, I think having a `to_pathname` would be confusing.

Anyway, consider concatenation:

```ruby
require "pathname"
a = Pathname.new("a") # => #<Pathname:a>
a + "b" # => #<Pathname:a/b>
a + Object.new # => TypeError: no implicit conversion of Object into String
```

Mirroring this behavior in `relative_path_from` can be accomplished with a simpler patch:

```diff
diff --git a/ext/pathname/lib/pathname.rb b/ext/pathname/lib/pathname.rb
index e61aa2c..e613afd 100644
--- a/ext/pathname/lib/pathname.rb
+++ b/ext/pathname/lib/pathname.rb
@@ -490,7 +490,7 @@ def each_child(with_directory=true, &b)
   #
   def relative_path_from(base_directory)
     dest_directory = self.cleanpath.to_s
-    base_directory = base_directory.cleanpath.to_s
+    base_directory = Pathname.new(base_directory).cleanpath.to_s
     dest_prefix = dest_directory
     dest_names = []
     while r = chop_basename(dest_prefix)
```

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

* 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/