Case vs using if question

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

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

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

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.

···

On Nov 16, 2006, at 11:07 PM, Scott Comboni wrote:

--
Eric Hodel - drbrain@segment7.net - http://blog.segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com

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

[mailto:list-bounce@example.com] On Behalf Of Scott Comboni:
# 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..

100 is quite many, therefore justin's suggestion of hash is good. You might also want to save the hash in a separate text file preferably in yaml format. This way if you need to add or delete codes, you just edit the yaml file (your program stay as is).

kind regards -botp

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

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

···

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

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

···

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

--
Lou.

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 :slight_smile:
I will give this a try today.
Sc-

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