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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
E15af4b1b2bd121942572a8b981ef84c?d=identicon&s=25 Morgan Cheng (Guest)
on 2007-06-05 11:56
(Received via mailing list)
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?
F1d6cc2b735bfd82c8773172da2aeab9?d=identicon&s=25 Nobuyoshi Nakada (Guest)
on 2007-06-05 13:02
(Received via mailing list)
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)/)
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2007-06-05 13:10
(Received via mailing list)
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
This topic is locked and can not be replied to.