mc9
mc9

Reputation: 6349

Pathname with Regex

I would like to know how to use Regex when instantiating a new Pathname.

I am instantiating a Pathname and passing it to FileUtils#rm_rf to delete the file. The problem I am trying to solve is to remove files that have a certain name without regard to extension:

See this contrived example:

target = Pathname.new(["#{@app_name}/#{@file_name}"])
FileUtils.rm_rf(target)

@file_name does not include extensions such as .rb or html.erb, but I would like to match all files with name equal to @file_name no matter what extensions they use.

My initial approach was to use Regex. But how can I use it, or any other suggestions?

Upvotes: 0

Views: 257

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626738

You can use Dir.Glob like this:

   Dir.glob("#{@app_name}/#{@file_name}.*").each { |f| File.delete(f) }

See more on that at http://ruby-doc.org/core-2.2.1/Dir.html#method-c-glob

Upvotes: 1

Related Questions