case vs using if question

Discussion in 'Ruby' started by Scott Comboni, Nov 17, 2006.

  1. 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, Nov 17, 2006
    #1
    1. Advertising

  2. 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
    Justin Collins, Nov 17, 2006
    #2
    1. Advertising

  3. 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/.
    Scott Comboni, Nov 17, 2006
    #3
  4. Scott Comboni

    Eric Hodel Guest

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

    http://trackmap.robotcoop.com
    Eric Hodel, Nov 17, 2006
    #4
  5. 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
    Robert Klemme, Nov 17, 2006
    #5
  6. On 11/17/06, Robert Klemme <> 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.
    Louis J Scoras, Nov 17, 2006
    #6
  7. Lou Scoras wrote:
    > On 11/17/06, Robert Klemme <> 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/.
    Scott Comboni, Nov 17, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve Franks
    Replies:
    2
    Views:
    1,247
    Steve Franks
    Jun 10, 2004
  2. Tee
    Replies:
    3
    Views:
    7,793
    Herfried K. Wagner [MVP]
    Jun 23, 2004
  3. Janice

    lower case to upper case

    Janice, Dec 10, 2004, in forum: C Programming
    Replies:
    17
    Views:
    1,172
    Richard Bos
    Dec 14, 2004
  4. Replies:
    1
    Views:
    2,474
    Mark P
    Apr 6, 2007
  5. darrel

    'case' vs. 'case is'

    darrel, Aug 14, 2008, in forum: ASP .Net
    Replies:
    3
    Views:
    274
    Rory Becker
    Aug 14, 2008
Loading...

Share This Page