0

有一个代码

file_paths = {nature:[], nature_thumb:[]}

运行良好的 Elsif 版本:

 Find.find('public/uploads') do |path|
   if path =~ /.*nature.*\.(jpg|png|gif)$/ and  path !~  /.*nature\/thumb.*\.(jpg|png|gif)$/
     file_paths[:nature] << path
   elsif
     path =~ /.*nature\/thumb.*\.(jpg|png|gif)$/
     file_paths[:nature_thumb] << path
     #etc
   end 
 end

案例版本导致问题

 Find.find('public/uploads') do |path|
   case 
   when path =~ /.*nature.*\.(jpg|png|gif)$/, path !~ /.*nature\/thumb.*\.(jpg|png|gif)$/ 
     file_paths[:nature] << path
   when path =~ /.*nature\/thumb.*\.(jpg|png|gif)$/
     file_paths[:nature_thumb] << path
     # etc
   end
 end

用 '&&' 代替逗号会导致错误。逗号工作错误。如何避免这种情况?

4

2 回答 2

2

您的案例陈述应如下所示:

Find.find('public/uploads') do |path|

   case 
       #Surround your statement with parenthesis
       when ((path =~ /.*nature.*\.(jpg|png|gif)$/) && (path !~ /.*nature\/thumb.*\.(jpg|png|gif)$/)) 
         file_paths[:nature] << path
       when path =~ /.*nature\/thumb.*\.(jpg|png|gif)$/
         file_paths[:nature_thumb] << path
         # etc
       end
    end  
于 2013-02-26T18:21:10.360 回答
2

更改案例的顺序:

Find.find('public/uploads') do |path|
  case path
  when /.*nature\/thumb.*\.(jpg|png|gif)$/
    file_paths[:nature_thumb] << path
  when /.*nature.*\.(jpg|png|gif)$/
    file_paths[:nature] << path
  end
end

或在您的情况下更好:

Find.find('public/uploads') do |path|
  file_paths[
    case path
    when /.*nature\/thumb.*\.(jpg|png|gif)$/ then :nature_thumb
    when /.*nature.*\.(jpg|png|gif)$/        then :nature
    end
  ] << path
end
于 2013-02-26T19:21:17.017 回答