summaryrefslogtreecommitdiff
path: root/sample
diff options
context:
space:
mode:
authorThomas R. Koll <tomk32@tomk32.de>2023-02-15 04:41:53 +0100
committerGitHub <noreply@github.com>2023-02-15 12:41:53 +0900
commitf03dd4ee778cbed7ea9510385b9bbdbdcf6d485b (patch)
tree41e0dd4faf4ebcb326755cd406d94388a2049ab9 /sample
parent3376eca80a9e23c295a4fe5fb9049c1ad27bb562 (diff)
downloadruby-f03dd4ee778cbed7ea9510385b9bbdbdcf6d485b.tar.gz
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>
Diffstat (limited to 'sample')
-rw-r--r--sample/dir.rb11
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