Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > case vs using if question

Reply
Thread Tools

case vs using if question

 
 
Scott Comboni
Guest
Posts: n/a
 
      11-17-2006
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

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Justin Collins
Guest
Posts: n/a
 
      11-17-2006
Scott Comboni wrote:
> 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



 
Reply With Quote
 
 
 
 
Scott Comboni
Guest
Posts: n/a
 
      11-17-2006
Paul Lutus wrote:
> Scott Comboni wrote:
>
> / ...
>
>> or am i better using
>> if component == 'PC': puts "Postcard"
>> elsif component == 'DC': puts "Decal"

>
> 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-

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Eric Hodel
Guest
Posts: n/a
 
      11-17-2006
On Nov 16, 2006, at 11:07 PM, Scott Comboni wrote:
> 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.

--
Eric Hodel - http://www.velocityreviews.com/forums/(E-Mail Removed) - http://blog.segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com



 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      11-17-2006
On 17.11.2006 08:21, Scott Comboni wrote:
> Paul Lutus wrote:
>> Scott Comboni wrote:
>>
>> / ...
>>
>>> or am i better using
>>> if component == 'PC': puts "Postcard"
>>> elsif component == 'DC': puts "Decal"

>> 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..


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
 
Reply With Quote
 
Louis J Scoras
Guest
Posts: n/a
 
      11-17-2006
On 11/17/06, Robert Klemme <(E-Mail Removed)> wrote:
>
> 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.


--
Lou.

 
Reply With Quote
 
Scott Comboni
Guest
Posts: n/a
 
      11-17-2006
Lou Scoras wrote:
> On 11/17/06, Robert Klemme <(E-Mail Removed)> wrote:
>>
>> 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.


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-


--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
case insensitive find on case sensitive stl map benhoefer@gmail.com C++ 1 04-06-2007 08:42 PM
lower case to upper case Janice C Programming 17 12-14-2004 02:35 PM
how to case select with case-insensitive string ? Tee ASP .Net 3 06-23-2004 07:40 PM
Possible to turn on/off cookieless sessions dynamically on a case by case basis at run-time? Steve Franks ASP .Net 2 06-10-2004 02:04 PM
Scorsese Collection: Keep case vs Snap case Ray DVD Video 0 05-30-2004 04:04 AM



Advertisments