In Case You Missed It

15 Jun

In all my time programming, I’ve never really used the “case” statement until yesterday. I was working on the following problem:

Write a program with a “smiley” method that accepts a hash as an argument. If the hash element with the key :mood has the value “happy”, then return”:)”. If it has the value “sad”, return “:(“. Otherwise, return “:|”.

Here are the solutions using the If vs Case statement:


The typical “if” statement solution:


def smiley(hash = {})
    mood = hash[:mood]
    if mood == "happy"
      ":)"
    elsif mood == "sad"
      ":("
    else
      ":|"
    end
end

Now here is the solution to the same problem using the “case” statement:

def smiley(hash = {})
    case hash[:mood]
    when "happy"
        ":)"
    when "sad"
        ":("
    else
        ":|"
    end
end

As you can see, the case statement is a bit more intuitive to read and it takes out the constant repetition of the “mood ==” in the if statement.

Advertisements

4 Responses to “In Case You Missed It”

  1. Craig Little June 17, 2012 at 12:04 am #

    Mentor here. 🙂

    Note that whens in switches are typically indented at the same level as case in idiomatic Ruby style.

    i.e.

    case variable_under_test
    when scenario_1
    “result 1”
    when scenario_2
    “result 2”
    end

    Also, you have a couple syntax errors in the case statement version: (1) hash[mood] should be hash[:mood] (this applies to the if statement version as well); and, (2) you have an extra end in there.

    I know this is a little demonstration method, but methods that take hash arguments typically set an empty hash as a default argument to make it more flexible. This means that it could be called without arguments and return “:|” in this case, which would seem like a reasonable return value instead of flipping out with ArgumentError.

    Hope that’s helpful!

    • Natasha Murashev June 17, 2012 at 6:58 am #

      Thanks Craig! I’ll take out the extra end and indent correctly 🙂

      The reason I didn’t make mood a symbol is because the mood is passed in as a string according to the assignment requirements. I modified the requirements and the code above though. Let me know if you see any other errors!

      • Craig Little (@craiglittle) June 17, 2012 at 10:15 pm #

        When a hash is being used to pass in arguments, symbols are always used for the keys in idiomatic Ruby (i.e. { :mood => ‘happy’ }). You’re correct that the *value* of the hash is expected to be a string, but the *key* should be a symbol. In any case, it was neither in your first iterations, which was going to give you an unknown variable error since you were referencing a variable (mood) that had not been defined.

        Looks like you’ve got it all worked out in the post now, though!

  2. Craig Little (@craiglittle) June 17, 2012 at 12:05 am #

    Mentor here. 🙂

    Note that whens in switches are typically indented at the same level as case in idiomatic Ruby style.

    i.e.

    case variable_under_test
    when scenario_1
    “result 1”
    when scenario_2
    “result 2”
    end

    Also, you have a couple syntax errors in the case statement version: (1) hash[mood] should be hash[:mood] (this applies to the if statement version as well); and, (2) you have an extra end in there.

    I know this is a little demonstration method, but methods that take hash arguments typically set an empty hash as a default argument to make it more flexible. This means that it could be called without arguments and return “:|” in this case, which would seem like a reasonable return value instead of flipping out with ArgumentError.

    Hope that’s helpful!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s