# Problem with reject! function for array

**URL:** <https://forum.crystal-lang.org/t/problem-with-reject-function-for-array/4731>\
**Category:** Help & Support\
**Created:** [June 27, 2022, 6:25pm UTC](https://forum.crystal-lang.org/t/problem-with-reject-function-for-array/4731 "2022-06-27T18:25:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fulgurance](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/fulgurance/32/1463_2.png) [@Fulgurance](https://forum.crystal-lang.org/u/Fulgurance)\
**Post date:** [June 27, 2022, 6:25pm UTC](https://forum.crystal-lang.org/t/problem-with-reject-function-for-array/4731/1 "2022-06-27T18:25:03Z")

</div>

Hello, actually I have 2 functions like that in one of my class implementation:

```auto
        def deleteFile(path : String)
            begin
                File.delete(path)
                puts path
            rescue error
                Ism.notifyOfDeleteFileError(path)
                pp error
                exit 1
            end
        end

        def deleteAllHiddenFiles(path : String)
            begin
                (Dir.children(path+"/").select! {|f| File.file? f}).each do |file|
                
                    puts path
                    puts file
                    puts file.starts_with?(".")
                    
                    if file.starts_with?(".")
                        deleteFile(path+"/"+file)
                        puts path+"/"+file
                    end
                end
            rescue error
                Ism.notifyOfDeleteAllHiddenFilesError(path)
                pp error
                exit 1
            end
        end

```

But strangely, when my program start to use deleteAllHiddenFiles(path : String) function, it don’t return what I expected, because it exclude the hidden file .gitignore from the list.

```auto
The path actually contain this:
zohran@zohran-VirtualBox:~/ism$ ls -la /mnt/sources/Linux-API-Headers-5.13.12/linux-5.13.12/usr/include/
total 88
drwxr-xr-x 13 zohran zohran 4096 Jun 27 19:19 .
drwxr-xr-x 3 zohran zohran 4096 Aug 18 2021 ..
drwxr-xr-x 2 zohran zohran 4096 Jun 27 19:19 asm
drwxr-xr-x 2 zohran zohran 4096 Jun 27 19:19 asm-generic
drwxr-xr-x 2 zohran zohran 4096 Jun 27 19:19 drm
-rw-r--r-- 1 zohran zohran 44 Aug 18 2021 .gitignore
drwxr-xr-x 29 zohran zohran 36864 Jun 27 19:19 linux
drwxr-xr-x 3 zohran zohran 4096 Jun 27 19:19 misc
drwxr-xr-x 2 zohran zohran 4096 Jun 27 19:19 mtd
drwxr-xr-x 3 zohran zohran 4096 Jun 27 19:19 rdma
drwxr-xr-x 3 zohran zohran 4096 Jun 27 19:19 scsi
drwxr-xr-x 3 zohran zohran 4096 Jun 27 19:19 sound
drwxr-xr-x 2 zohran zohran 4096 Jun 27 19:19 video
drwxr-xr-x 2 zohran zohran 4096 Jun 27 19:19 xen

```

If I remove the select! function, I have everything, but if I use select!, in result, I have an empty array. Why ?

My goal is to keep only the list of all files and exclude all directories (before I will check if the file is an hidden file)

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [June 27, 2022, 7:48pm UTC](https://forum.crystal-lang.org/t/problem-with-reject-function-for-array/4731/2 "2022-06-27T19:48:58Z")

</div>

> [@Fulgurance](#):
>
> My goal is to keep only the list of all files and exclude all directories (before I will check if the file is an hidden file)

I think you could do what you want via like:

```auto
def delete_file(path : String)
  File.delete(path)
rescue error
  Ism.notifyOfDeleteFileError(path)
  exit 1
end

def delete_all_hidden_files(path : String)
  Dir.glob("#{path}/.*", match_hidden: true) do |file_path|
    next unless File.file? file_path

    delete_file file_path
  end
rescue error
  Ism.notifyOfDeleteAllHiddenFilesError(path)
  exit 1
end

```

This will iterate over all hidden files/directories, calling the delete function for each hidden file, skipping directories.

---

<div class="post-metadata">

**Author:** ![Fulgurance](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/fulgurance/32/1463_2.png) [@Fulgurance](https://forum.crystal-lang.org/u/Fulgurance)\
**Post date:** [June 27, 2022, 9:09pm UTC](https://forum.crystal-lang.org/t/problem-with-reject-function-for-array/4731/3 "2022-06-27T21:09:53Z")

</div>

Oh thanks you ! This work better definitely !
