Using case statements to declare variables

February 21, 2016   

If you wanted to assign different variables depending on several different situations, you might be tempted to do so like this:

if model == ‘Apple MacBook Air 13.3’
  price = 999.00
elsif model == ‘Apple MacBook Pro with Retina Display 13.3’
  price = 1169.00
elsif model == ‘Apple MacBook Pro with Retina Display 15’
  price = 1849.00
else
  price = “Could not find model”
end

In these situations, ruby offers a more elegant solution using case statements to do the same thing. It turns out that you can use case statements to declare variables like this:

price = case model
        when ‘Apple MacBook Air 13.3’ then 999.00
        when ‘Apple MacBook Pro with Retina Display 13.3’ then 1169.00
        when ‘Apple MacBook Pro with Retina Display 15’ then 1849.00
        else “Could not find model”
        end

This is a quick little trick that you could use in this common situation.

Happy coding! :)