dgosh
dgosh

Reputation: 15

Ruby Case Statements with Multiple conditions

I have a method that returns image size based upon the users selection and now I want to add another condition to my case statement. It is not setting the correct image size when I call the method again after my system call doing pdfinfo if the user chose STANDARD it should be 1250x1075 but it does not even do my case statement, it directly goes to else and sets 1728x1075

This is what I've tried

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

This is where I call my method

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)

Upvotes: 0

Views: 1371

Answers (4)

Cary Swoveland
Cary Swoveland

Reputation: 110685

@Jörg has explained the problem with your code. You might consider writing your method as follows.

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

Upvotes: 0

Jörg W Mittag
Jörg W Mittag

Reputation: 369458

STANDARD && (pdf_size != LEGAL_PDF_SIZE), FINE && pdf_size != LEGAL_PDF_SIZE, SUPERFINE && pdf_size != LEGAL_PDF_SIZE, FINE && pdf_size == LEGAL_PDF_SIZE, and SUPERFINE && pdf_size == LEGAL_PDF_SIZE are all booleans, but resolution is a String, so they will never match.

[STANDARD, (pdf_size === LEGAL_PDF_SIZE)] is an Array. An Array will never match a String.

So, therefore, none of your cases will ever match, and you will always fall into the else case.

Upvotes: 2

dbugger
dbugger

Reputation: 16399

I'd create an array of hashes...

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"}]

and then change FaxCall.set_image_size(resolution, pdf_size=nil) to look up the matching hash and grab the image size.

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

Easier to read and to add both more map values and extra criteria.

Upvotes: 0

Abhishek Prusty
Abhishek Prusty

Reputation: 864

That's because STANDARD and ELSE have the same image size.

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

See what I mean ?

Upvotes: 0

Related Questions