Project

General

Profile

Actions

Feature #16621

open

Second block parameter of Pathname#glob to be relative path from self

Added by 5.5 (5 5) about 4 years ago. Updated about 4 years ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:97110]

Description

I often write such a code:

some_dir = Pathname("some/dir")
some_dir.glob("**/*.html") do |path|
  rel_path = path.relative_path_from(some_dir)
  # ...
end

If Pathname#glob yields the relative path from self as a second block parameter, I can write the above as follows:

Pathname("some/dir").glob("**/*.html") do |path, rel_path|
  # ...
end

Updated by shevegen (Robert A. Heiler) about 4 years ago

(Just trying to get some more details - I have no particular opinion per se as I
tend to almost exclusively use Dir[] myself, so this is more for ruby users who
use Pathname in general.)

Would this change have any potential backwards incompatibility/problem? This is
a genuine question, by the way, I really don't know, since I don't use Pathname
since many years, and I have no pro/con opinion about the suggestion itself.

Updated by 5.5 (5 5) about 4 years ago

I think no problem in the backward compatibility.
If we use the proposed glob method with one block parameter, it behaves same as the current glob method.

Updated by Dan0042 (Daniel DeLorme) about 4 years ago

What you want is this:

Dir.glob("**/*.html", base: "some/dir")

It would be nice if this was exposed via Pathname. There's something weird with the documentation for Pathname#glob though; it says "This method uses the base keyword argument of Dir.glob." but this doesn't appear to be true. Dir.glob(g, base: p) is not the same as p.glob(g)

Updated by jeremyevans0 (Jeremy Evans) about 4 years ago

5.5 (5 5) wrote in #note-2:

I think no problem in the backward compatibility.
If we use the proposed glob method with one block parameter, it behaves same as the current glob method.

This is incorrect if the block passed is a lambda.

Updated by 5.5 (5 5) about 4 years ago

jeremyevans0 (Jeremy Evans) wrote in #note-4:

This is incorrect if the block passed is a lambda.

Oops. You are right.
My proposal contains the backward incompatibility.
So some option is needed ...?

Updated by 5.5 (5 5) about 4 years ago

Dan0042 (Daniel DeLorme) wrote in #note-3:

What you want is this:

Dir.glob("**/*.html", base: "some/dir")

Thank you for your suggestion.
But I want both its absolute path and its relative path from self.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0