From f03dd4ee778cbed7ea9510385b9bbdbdcf6d485b Mon Sep 17 00:00:00 2001 From: "Thomas R. Koll" Date: Wed, 15 Feb 2023 04:41:53 +0100 Subject: 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 --- sample/dir.rb | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) (limited to 'sample') 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 -- cgit v1.2.1