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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
You could try: directory = Dir.open("#{RAILS_ROOT}/public/folder") @files_and_folders = directory.to_a.select {|i| /(^\..?$)|(.svn $)/.match(i) } On Feb 1, 12:42 pm, rph <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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. > > 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. > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 2/1/07, rph <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---