Wrong selected Item with curselection

So I am very new to Ruby and Tk and I have a Problem, which solution I
didn’t find after hours of research:

I have a directory which contains many projects.
With the following code I insert the names of the existing projects into
a Listbox:

$projects = Dir.entries("#{Dir.pwd}/projects")[2…-1]
$projects.each {|f| $list.insert ‘end’, f}

I created a searchbar with a button which allows to find a specific
entry:

def searchprojects
searchedprojects = []
$list.clear()
if $spec_ent.to_s.empty?
$projects.each {|f| $list.insert ‘end’, f}
else
$projects.each {|i|
i = i.to_s
searchedprojects.insert(searchedprojects.length(), i) if
i.downcase.include? $spec_ent.to_s.downcase}
searchedprojects.each {|f| $list.insert ‘end’, f}
end
end

And with the following code I chose the selected project (which shall be
filtered before):

def show_project

updates the status of the chosen project

idx = $list.curselection[0]
$proj_name = $projects[idx]
$status.value = “Project chosen: #{$proj_name}”
end

My Problem: After I filtered the Listbox with my searchbar, and I chose
a project in the Listbox, I get the wrong drectory(name), because of the
index. For instance, when i filter the Listbox, it contains only 3
entries, and I chose the third, it will give me the third entry of the
unfiltered Listbox.

Thanks for helping.