[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

case vs using if question

Scott Comboni

11/17/2006 7:07:00 AM

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

8 Answers

Paul Lutus

11/17/2006 7:14:00 AM

0

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.

--
Paul Lutus
http://www.ara...

Justin Collins

11/17/2006 7:18:00 AM

0

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



Scott Comboni

11/17/2006 7:21:00 AM

0

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

Eric Hodel

11/17/2006 7:40:00 AM

0

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 - drbrain@segment7.net - http://blog.se...
This implementation is HODEL-HASH-9600 compliant

http://trackmap.rob...



Paul Lutus

11/17/2006 8:16:00 AM

0

Scott Comboni wrote:

> Paul Lutus wrote:

/ ...

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

Oh, that's something different! In such a case, you should consider writing
something called a "dispatcher" for the sake of efficiency and
maintainability.

This is an example where stating the problem fully at the outset can really
help in finding a solution. At the level of 100 comparisons, you really
don't want to use "if ... else" or "case ...when" either. There are simply
too many comparisons for efficient use of either of the discussed
alternatives.

Here is an example dispatcher, with just a few defined command resolutions,
just to show you the pattern:

------------------------------------------

#!/usr/bin/ruby -w

class Dispatcher

def initialize()
@dispatch_hash = {
"a" => :first_case,
"b" => :second_case,
"c" => :third_case
}
end

def first_case()
puts "first"
end

def second_case()
puts "second"
end

def third_case()
puts "third"
end

def default()
puts "unrecognized"
end

def dispatch(*s)
s.each do |com|
send(@dispatch_hash[com] || :default)
end
end

end

disp = Dispatcher.new

disp.dispatch("a","b","c","d")

------------------------------------------

Output:

first
second
third
unrecognized

The idea of this design is to maximize the speed and efficiency of
dispatching entered commands, without having to write a long, inefficient
series of "if ... else" or "case ... when" structures. It is useful when
the number of commands becomes too large to handle efficiently any other
way.

This dispatcher will connect your entered string to its associated command
very quickly, and it is easy to write and maintain. It is much easier to
understand and add to than the alternatives we discussed earlier.

--
Paul Lutus
http://www.ara...

Robert Klemme

11/17/2006 9:22:00 AM

0

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

Louis J Scoras

11/17/2006 12:09:00 PM

0

On 11/17/06, Robert Klemme <shortcutter@googlemail.com> 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.

Scott Comboni

11/17/2006 5:30:00 PM

0

Lou Scoras wrote:
> On 11/17/06, Robert Klemme <shortcutter@googlemail.com> 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-....