[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

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

Morgan Cheng

6/5/2007 9:54:00 AM

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?

2 Answers

Nobuyoshi Nakada

6/5/2007 11:02:00 AM

0

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)/)

--
Nobu Nakada

Robert Klemme

6/5/2007 11:07:00 AM

0

On 05.06.2007 11:54, Morgan Cheng wrote:
> 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