[ENGLISH VERSION] Ruby Case

Hey, how are you!?!

Well, today I bring the way how Ruby treats it’s switch case. Let’s go!

The easy way is:

greeting = 'hey'

case greeting
when 'hey'
  puts 'how are you?'
when 'hi'
  puts 'hello'
else puts 'i don\'t understand'
end

The case must end with an end, showing that the condition is done and each case is tested with a when statement.


THEN

Now the variations. The first one is with the then statement, that may have multiple lines or not. The same way it would be if it wasn’t there.

greeting = 'hey'

case greeting
when 'hey' then puts 'how are you?'
when 'hi' then
  puts 'hello'
when 'ahoy'
  then puts 'ahoy back'
when 'heyhey'
  then
    puts 'heyheyhey'
end

Here we have a code intentionally messed up using then, that if we “humanize” it we have: when something happen then do this.

Testing the code we notice that all “whens” work, but visually disturbs, so pick the best one for you!


ON

The second variation brings something extremely cool in Ruby: the test of multiple conditions.
So you ask me: But isn’t it what case do? Yes, yes, it is, but here we can test inside when also. See:

greeting = 'hey'

case greeting
when 'hey', 'Hey'
  puts 'how are you?'
when 'hi', 'Hi'
  puts 'hello'
else puts 'i don\'t understand'
end

The multiple test sees if a String starts or no with a capital letter, cool hun?


EXIT

It is possible to use a keyword to indicate the end of a condition, but with a brief test I’ve noticed that is we are on irb, Ruby’s terminal, irb will finish the execution as soon as a condition is tested.

greeting = 'hey'

case greeting
when 'hey', 'Hey'
  puts 'how are you?'
  exit
when 'hi', 'Hi'
  puts 'hello'
  exit
else puts 'i don\'t understand'
end

The exit must not be used after then, an error is raised.


RETURNING A VALUE

Last but not least, we know that the last line written in some parts of our code counts as a return statement, the same applies for cases.

car = 'Patriot'

manufacturer = case car
   when 'Focus'     then 'Ford'
   when 'Navigator' then'Lincoln'
   when 'Camry'     then 'Toyota'
   when 'Civic'     then 'Honda'
   when 'Patriot'   then 'Jeep'
   when 'Jetta'     then 'VW'
   when 'Ceyene'    then 'Porsche'
   when 'Outback'   then 'Subaru'
   when '520i'      then 'BMW'
   when 'Tundra'    then 'Nissan'
   else 'Unknown'
end

puts 'The' + car  + ' is made by '  + manufacturer
    #=> The Patriot is made by Jeep

That’s all folks! Play a little and have fun with Ruby (let me see it on comments!)

See ya!

rubycaseENG

Anúncios

Um comentário sobre “[ENGLISH VERSION] Ruby Case

Diga alguma coisa ... suas dúvidas quem sabe!

Preencha os seus dados abaixo ou clique em um ícone para log in:

Logotipo do WordPress.com

Você está comentando utilizando sua conta WordPress.com. Sair / Alterar )

Imagem do Twitter

Você está comentando utilizando sua conta Twitter. Sair / Alterar )

Foto do Facebook

Você está comentando utilizando sua conta Facebook. Sair / Alterar )

Foto do Google+

Você está comentando utilizando sua conta Google+. Sair / Alterar )

Conectando a %s