case vs using if question

S

Scott Comboni

Hello all,
Nuby question?
I need to parse the syntax on files based on a two charcter code such as
PC which would return a value like this PC = Postcard I have many many
codes and Im very new to Ruby and not sure what the best way to do this
is and performance considerations? So would multiple if statements be a
better ruby way then using case or does it mater?

example:
file name: d123456_PC_xxxxx.pdf

filename.split('_')[1]

case component
when "PC": puts "Postcard"
when "DC": puts "Decal"
else
puts "n/a"
end

or

or am i better using
if component == 'PC': puts "Postcard"
elsif component == 'DC': puts "Decal"

....etc
 
J

Justin Collins

Scott said:
Hello all,
Nuby question?
I need to parse the syntax on files based on a two charcter code such as
PC which would return a value like this PC = Postcard I have many many
codes and Im very new to Ruby and not sure what the best way to do this
is and performance considerations? So would multiple if statements be a
better ruby way then using case or does it mater?

example:
file name: d123456_PC_xxxxx.pdf

filename.split('_')[1]

case component
when "PC": puts "Postcard"
when "DC": puts "Decal"
else
puts "n/a"
end

or

or am i better using
if component == 'PC': puts "Postcard"
elsif component == 'DC': puts "Decal"

....et

I would certainly use a case statement over a bajillion if statements.
You could also (depending on your problem) put all the codes in a hash
table and just do a lookup that:

codes = { "PC" => "Postcard",
"DC" => "Decal",
...etc }

filenames.each do filename
puts codes[filename.split("_")[1]]
end

Or similar.

-Justin
 
S

Scott Comboni

Paul said:
Scott Comboni wrote:

/ ...


It depends to some extent on how many comparisons there are. IMHO you
should
consider using the case ... when construction for more than a few
comparisons.

Also, look at case ... when ... then sample code online for the correct
syntax to use.

Thanks so much Paul for the quick response.. I have about 100 codes to
search I just converted it to case seems a little cleaner and less
typing which is good.. Thanks for the info..

s-
 
E

Eric Hodel

I need to parse the syntax on files based on a two charcter code
such as
PC which would return a value like this PC = Postcard I have many
many
codes and Im very new to Ruby and not sure what the best way to do
this
is and performance considerations?

Benchmark and profile to find performance considerations.
So would multiple if statements be a better ruby way then using
case or does it mater?

Use multiple if statements when the comparisons are different.

Use a case statement when one side of the comparison is always the
same or when exploiting #=== will make your code simpler.

But see also the Hash solution for when you're using a case for a
map. That's even cleaner and simpler than a big case statement.
 
R

Robert Klemme

Thanks so much Paul for the quick response.. I have about 100 codes to
search I just converted it to case seems a little cleaner and less
typing which is good.. Thanks for the info..

In that case (!) I would definitively use a Hash as Justin suggested.
This is /much/ more efficient and also you gain flexibility in filling
that Hash (i.e. load it from some config file vs. making it part of the
code) if you need that.

Kind regards

robert
 
L

Louis J Scoras

In that case (!) I would definitively use a Hash as Justin suggested.
This is /much/ more efficient and also you gain flexibility in filling
that Hash (i.e. load it from some config file vs. making it part of the
code) if you need that.

Kind regards

robert

Agreed. You can also extend a table solution at runtime if need be.
 
S

Scott Comboni

Lou said:
Agreed. You can also extend a table solution at runtime if need be.

Great and thanks for everyones help and sample code snippets hopefully
in time I can help out :)
I will give this a try today.
Sc-
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top