diff options
author | Thomas R. Koll <tomk32@tomk32.de> | 2023-02-15 04:41:53 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-02-15 12:41:53 +0900 |
commit | f03dd4ee778cbed7ea9510385b9bbdbdcf6d485b (patch) | |
tree | 41e0dd4faf4ebcb326755cd406d94388a2049ab9 /sample | |
parent | 3376eca80a9e23c295a4fe5fb9049c1ad27bb562 (diff) |
Refactor dir.rb sample (#6977) [ci skip]
* Refactor dir.rb sample
The original (1998) sample with a for-loop and use of case/when isn't what we'd write nowadays
* [DOC] Update sample/dir.rb [ci skip]
Do not leave a `Dir` opened.
* [DOC] Update sample/dir.rb [ci skip]
Fix ArgumentError.
---------
Co-authored-by: Nobuyoshi Nakada <nobu@ruby-lang.org>
Notes
Notes:
Merged-By: hsbt <hsbt@ruby-lang.org>
Diffstat (limited to 'sample')
-rw-r--r-- | sample/dir.rb | 11 |
1 files changed, 3 insertions, 8 deletions
diff --git a/sample/dir.rb b/sample/dir.rb index 0c55078973..81257cd917 100644 --- a/sample/dir.rb +++ b/sample/dir.rb @@ -1,12 +1,7 @@ # directory access # list all files but .*/*~/*.o -dirp = Dir.open(".") -for f in dirp - case f - when /\A\./, /~\z/, /\.o\z/ - # do not print - else - print f, "\n" +Dir.foreach(".") do |file| + unless file.start_with?('.') or file.end_with?('~', '.o') + puts file end end -dirp.close |