How add "Not-match" logic into Enumerable.grep?

M

Morgan Cheng

Say, I want to get all files in one folder. Since Dir.entries will
have "." and ".." included. I want to strip them out with "grep". I
tried to write in such a way as I can did similarly in Perl:
log_files = Dir.entries(log_dir).grep(!/^\.\.?/)
But, it is a syntax error to have "!/regex/" as grep's argument.

Should I use some trick to make grep accept "not-match" regex?
 
N

Nobuyoshi Nakada

Hi,

At Tue, 5 Jun 2007 18:55:02 +0900,
Morgan Cheng wrote in [ruby-talk:254395]:
Say, I want to get all files in one folder. Since Dir.entries will
have "." and ".." included. I want to strip them out with "grep". I
tried to write in such a way as I can did similarly in Perl:
log_files = Dir.entries(log_dir).grep(!/^\.\.?/)

grep(/\A(?!\.\.?\z)/)
 
R

Robert Klemme

Say, I want to get all files in one folder. Since Dir.entries will
have "." and ".." included. I want to strip them out with "grep". I
tried to write in such a way as I can did similarly in Perl:
log_files = Dir.entries(log_dir).grep(!/^\.\.?/)
But, it is a syntax error to have "!/regex/" as grep's argument.

Should I use some trick to make grep accept "not-match" regex?

Use #reject:

Dir.entries(".").reject {|x| /\A\.+\z/ =~ x}

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top