Coda Chang Coda Chang - 1 month ago 11
Ruby Question

Rails write regular expression to get folder name

This is a simple question, I want to clean up carrierwave picture after the Rspec test.

The picture file is in

public/NewsletterImage_809abef471699025f5bb396d04dfd4f0"
, the string behind
NewsletterImage
will be a random string.

I want to get the folder name by
regular expression
, but it didn't work.

I've tried this

Dir["#{Rails.root}/public/^NewsletterImage"]
=> []
Dir["#{Rails.root}/public/NewsletterImage.*"]
=> []


I am confused, because I use Rubular and it can extract these names.

Answer

Dir[] doesn't take a regular expression, it takes a shell wildcard expression. These might look superficially similar, especially with .* being a common feature in both, but they're not the same.

If you want to use a regular expression you have to fetch all entries and then filter them with something like grep.

Comments