How to match a path with the first five characters [duplicate]

一笑奈何 提交于 2019-12-02 12:39:07

This will match a filenename with the first five characters and anything after that:

Dir.glob('/var/cache/acpchef/commv*') 
# will match /var/cache/acpchef/commv12345abcdxyz123456

This will match a filename with any three extra characters:

Dir.glob('/var/cache/acpchef/commv???') 
# will match /var/cache/acpchef/{commv123, commv456, commvabc, ...}

This will match a filename with three numbers:

Dir.glob('/var/cache/acpchef/commv[0-9][0-9][0-9]')
# will match /var/cache/acpchef/commv{123, 234, 456, 999, ...}

Also, your example is not correctly using the block syntax and has begin..end without rescue, which does essentially nothing. It should become:

Dir.glob('/var/cache/acpchef/commv???').each do |cwd_kernel|
  cmd = Mixlib::ShellOut.new("commandrun", :cwd => cwd_kernel)
  cmd.run_command
  log 'run'
end

Dir.glob returns an Array, which is a collection of the results. Array.each returns an Enumerator, which basically is an object that runs the following block of code with a new value as many times as there are new values available, which means you can use it to run the same block of code for all the results of Dir.glob. The value is passed to the block through the |block_argument| syntax.

The begin keyword in ruby is used to capture errors:

begin
  # do something that generates an exception
rescue => exception
  # handle the exception
end

A begin without rescue, ensure or else does nothing.

Also, this is quite similar to a previous question.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!