Regular expression and reading directories

I have to read in a list of folders and display them to the user.
However, there are certain folders that I don’t want to show up, and I’m
trying to restrict this using regular expressions, which I’m not so good
at.

There are things like “.” and “…” and “_svn” that show up, that I’m
trying to restrict.

Here’s my method (which isn’t working):

def view_directory
@files_and_folders = []
reg_exp = Regexp.new(/^$/) # what regexp goes in here?
Dir.foreach("#{RAILS_ROOT}/public/folder") do |f|
@files_and_folders << f if f != reg_exp
end
end

This may not even be the best way to handle this, as I’ve never dealt
with files/directories before. Any thoughts on the regular expression,
or a better way? Thanks.

You could try:

directory = Dir.open("#{RAILS_ROOT}/public/folder")
@files_and_folders = directory.to_a.select {|i| /(^…?$)|(.svn
$)/.match(i) }

On 2/1/07, rph [email protected] wrote:

I have to read in a list of folders and display them to the user.
However, there are certain folders that I don’t want to show up, and I’m
trying to restrict this using regular expressions, which I’m not so good
at.

There are things like “.” and “…” and “_svn” that show up, that I’m
trying to restrict.

Might this do?:

Dir["#{RAILS_ROOT}/public/folder/*"]

Dir.[] (and Dir.glob) are much like a unix shell glob in that it will
omit dotfiles unless the glob starts with a dot.

jdswift wrote:

You could try:

directory = Dir.open("#{RAILS_ROOT}/public/folder")
@files_and_folders = directory.to_a.select {|i| /(^…?$)|(.svn
$)/.match(i) }

I think this solution will work just fine, but it actually returns
exactly what I don’t want. How can I say to return the opposite?
Essentially…

@files_and_folders = !directory…

Thanks!

rph wrote:

jdswift wrote:

You could try:

directory = Dir.open("#{RAILS_ROOT}/public/folder")
@files_and_folders = directory.to_a.select {|i| /(^…?$)|(.svn
$)/.match(i) }

I think this solution will work just fine, but it actually returns
exactly what I don’t want. How can I say to return the opposite?
Essentially…

@files_and_folders = !directory…

Thanks!

Nevermind… I just used “reject” instead of “select”. I forgot about
that… thanks!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs