Case with strings

Discussion in 'Ruby' started by Aquila, Mar 19, 2005.

  1. Aquila

    Aquila Guest

    A similar problem:
    case key.strip
    when "c" "Synopsis"
    when "s" "Category"
    when "io" "Inputs and outputs"
    when "processing" "Processing type"
    when "d" "Description"
    when "n" "Notes"
    when "e" "Examples"
    when "h" "Element handlers"
    when "a" "See also"
    else puts "Unknown key"
    end
    if key.strip == "a"
    puts "This gives output"
    end
    unknown key
    This gives output

    What am I doing wrong? I'm a really Ruby newbie so probably I'm looking at
    it the wrong way...
    --
    "May the source be with you"
    Aquila, Mar 19, 2005
    #1
    1. Advertising

  2. Aquila wrote:

    > case key.strip
    > when "c" "Synopsis"
    > when "s" "Category"
    > when "io" "Inputs and outputs"
    > when "processing" "Processing type"
    > when "d" "Description"
    > when "n" "Notes"
    > when "e" "Examples"
    > when "h" "Element handlers"
    > when "a" "See also"
    > else puts "Unknown key"
    > end
    > if key.strip == "a"
    > puts "This gives output"
    > end
    > unknown key
    > This gives output
    >
    > What am I doing wrong? I'm a really Ruby newbie so probably I'm looking at
    > it the wrong way...


    You forget to put "puts" before you're Strings. This will work:

    case key.strip
    when "c" puts "Synopsis"
    when "s" puts "Category"
    ...
    end

    And this will also work and require less repetition:

    puts case key.strip
    when "c": "Synopsis"
    when "s": "Category"
    ...
    end

    Though I wonder if you're not better of with a Hash:

    puts({
    "c" => "Synopsis",
    "s" => "Category",
    ...
    }[key.strip])
    Florian Gross, Mar 19, 2005
    #2
    1. Advertising

  3. Aquila

    Aquila Guest

    Florian Gross wrote:

    <snip>
    > And this will also work and require less repetition:
    >
    > puts case key.strip
    > when "c": "Synopsis"
    > when "s": "Category"
    > ...
    > end
    >


    Actually the code has less duplication, this was just an example. But why
    doesn't this work:
    def keyToName(key)
    case key.strip
    ....
    when "h" "Element handlers"
    when "a" "See also"
    else "Unknown key"
    end
    end

    puts keyToName("a")
    puts keyToName('a')
    It gives "Unknown key" twice, instead of "See also" at least once.

    > Though I wonder if you're not better of with a Hash:
    >
    > puts({
    > "c" => "Synopsis",
    > "s" => "Category",
    > ...
    > }[key.strip])


    That sounds nice, is that code faster?

    Thanks for your help
    --
    "May the source be with you"
    Aquila, Mar 19, 2005
    #3
  4. Aquila wrote:

    > Actually the code has less duplication, this was just an example. But why
    > doesn't this work:
    > def keyToName(key)
    > case key.strip
    > ...
    > when "h" "Element handlers"
    > when "a" "See also"
    > else "Unknown key"
    > end
    > end
    >
    > puts keyToName("a")
    > puts keyToName('a')
    > It gives "Unknown key" twice, instead of "See also" at least once.


    Hah, I knew that you weren't supposed to use when like that. You're
    actually doing this with the above code:

    case key.strip
    when "hElement handlers"
    when "aSee also"
    else "Unknown key"
    end

    Which of course isn't what you want. Put a "then" or ":" between the
    condition and the action parts and it will work.

    >>Though I wonder if you're not better of with a Hash:
    >>
    >>puts({
    >> "c" => "Synopsis",
    >> "s" => "Category",
    >> ...
    >>}[key.strip])

    >
    > That sounds nice, is that code faster?


    It ought to be faster, especially when you assign the Hash to a
    constant, but I doubt that it will matter much.
    Florian Gross, Mar 19, 2005
    #4
  5. Aquila

    Malte Milatz Guest

    Aquila:
    > case key.strip
    > when "c" "Synopsis"


    # You need the keyword 'then' here.

    when 'c' then 'Synopsis'
    # or alternatively:
    when 'c'; 'Synopsis'

    # And you're likely to write something like this:

    answer = case key.strip
    when 'c' then 'Synopsis'
    # etc.
    end
    # now work with answer

    # Consider, too, using a hash. It does what you want in a very direct and
    # clean way.

    # Malte
    Malte Milatz, Mar 19, 2005
    #5
    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,251
    Steve Franks
    Jun 10, 2004
  2. Tee
    Replies:
    3
    Views:
    7,803
    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,177
    Richard Bos
    Dec 14, 2004
  4. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    757
    Malcolm
    Jun 24, 2006
  5. anonym
    Replies:
    1
    Views:
    1,018
    Knute Johnson
    Jan 15, 2009
Loading...

Share This Page