具有多个条件的Ruby案例声明

时间:2020-05-14 03:13:55

标签: ruby-on-rails ruby switch-statement case-statement

我有一个根据用户选择返回图像大小的方法,现在我想向我的case语句添加另一个条件。我在系统调用后pdfinfo再次调用该方法时,如果用户选择了STANDARD,它应该设置为1250x1075,但它没有设置正确的图像大小,但我的case语句甚至都没有执行,它将直接转到else并设置1728x1075

这是我尝试过的

205   def FaxCall.set_image_size(resolution, pdf_size=nil)
206     case resolution
207     when STANDARD && (pdf_size != LEGAL_PDF_SIZE)]
208       image="1728x1075"
209     when FINE && pdf_size != LEGAL_PDF_SIZE
210       image="1728x2150"
211     when SUPERFINE && pdf_size != LEGAL_PDF_SIZE
212       image="1728x4300"
213     when [STANDARD, (pdf_size === LEGAL_PDF_SIZE)]
214       image="1250x1720"
215     when FINE && pdf_size == LEGAL_PDF_SIZE
216       image="1700x2800"
217     when SUPERFINE && pdf_size == LEGAL_PDF_SIZE
218       image="3400x5572"
219     else
220       image="1728x1075"
221     end
222     return image
223   end

这是我调用方法的地方

135    def FaxCall.prepare_doc(in_file,out_file,res=STANDARD)
139     image = FaxCall.set_image_size(res)
140     res = STANDARD unless RESOLUTION_OPTIONS.values.include?(res)
145       if ext.eql?("pdf")
146         pdf_size = `pdfinfo "#{in_file}"  | grep 'Page size:'`.gsub(/Page size:\s*\b/, '').chomp
147         if pdf_size == LEGAL_PDF_SIZE
148           image = FaxCall.set_image_size(res,pdf_size)

4 个答案:

答案 0 :(得分:2)

kubectl get deployment nginx-app NAME READY UP-TO-DATE AVAILABLE AGE nginx-app 1/1 1 1 2m kubectl get po -l run=nginx-app NAME READY STATUS RESTARTS AGE nginx-app-2883164633-aklf7 1/1 Running 0 2m kubectl delete deployment nginx-app deployment "nginx-app" deleted kubectl get po -l run=nginx-app # Return nothing STANDARD && (pdf_size != LEGAL_PDF_SIZE)FINE && pdf_size != LEGAL_PDF_SIZESUPERFINE && pdf_size != LEGAL_PDF_SIZEFINE && pdf_size == LEGAL_PDF_SIZE都是布尔值,而SUPERFINE && pdf_size == LEGAL_PDF_SIZEresolution ,因此它们将永远不会匹配。

String[STANDARD, (pdf_size === LEGAL_PDF_SIZE)]Array将永远与Array不匹配。

因此,您的情况都不会匹配,并且您将始终属于String情况。

答案 1 :(得分:0)

这是因为using (StreamReader streamReader = new StreamReader(stream)) { while (true) { try { command = streamReader.ReadLine().Trim(); } catch (NullReferenceException) { blah-blah break; } } } STANDARD具有相同的图像尺寸。

ELSE
207     when STANDARD && (pdf_size != LEGAL_PDF_SIZE)]
208       image="1728x1075"

明白我的意思吗?

答案 2 :(得分:0)

我要创建一个哈希数组...

RES_MAP = [{res: STANDARD, legal: false, image: "1728x1075"},
  {res: FINE, legal: false , image: "1728x2150"},
  {res: SUPERFINE, legal: false , image: "1728x4300"},
  {res: STANDARD, legal: true, image: "1250x1720"},
  {res: FINE, legal: true , image: "1700x2800"},
  {res: SUPERFINE, legal: true , image: "3400x5572"}]

然后更改FaxCall.set_image_size(resolution, pdf_size=nil)来查找匹配的哈希并获取图像大小。

def FaxCall.set_image_size(resolution, pdf_size)
  is_legal = (pdf_size == LEGAL_PDF_SIZE)
  match_res = RES_MAP.select{ |r| r[:res] == resolution && r[:legal] == is_legal}.first
  return match_res.present? : match_res[:image] ? "1728x1075"
end

易于阅读,并添加更多地图值和额外条件。

答案 3 :(得分:0)

@Jörg用您的代码解释了问题。您可以考虑如下编写方法。

DEFAULT_IMAGE_SIZE = "1728x1075"

def FaxCall.set_image_size(resolution, pdf_size=nil)
  case pdf_size
  when LEGAL_PDF_SIZE
    case resolution
    when STANDARD  then "1250x1720"
    when FINE      then "1700x2800"
    when SUPERFINE then "3400x5572"
    else                DEFAULT_IMAGE_SIZE
    end
  else
    case resolution
    when STANDARD  then "1728x1075"
    when FINE      then "1728x2150"
    when SUPERFINE then "1728x4300"
    else                DEFAULT_IMAGE_SIZE
    end
  end
end
相关问题