從select中刪除哈希項

[英]Delete hash item from within select


Is there a way to delete a hash item from within a select? I want to check if a file exists in a directory and if it does, remove it from the hash.

有沒有辦法從選擇中刪除哈希項?我想檢查目錄中是否存在文件,如果存在,則將其從哈希中刪除。

hash = {"http://s3-us-west-2.amazonaws.com/packages/ics/ics_4.0.0.tar.gz"=>"5103909285b549eda0b6a13dd503790a", ...}

hash.select do |k,v|
  if File.file?("#{k[/[^\/]+$/]}")
    # Remove from hash

  end
end

1 个解决方案

#1


You cannot delete element in select, delete_if is a good choice.

你不能刪除select中的元素,delete_if是個不錯的選擇。

hash.delete_if { |k,_| File.file?("#{k[/[^\/]+$/]}") }

update:

hash_copy = hash.dup

hash.select do |k,v|
  if File.file?("#{k[/[^\/]+$/]}")
    hash_copy.delete(k)
    ......
  end
end

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/05/08/7257306753154c363f2dc97dc979f335.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com