Switch statement in Ruby

Code examples and video about swith statement in Ruby programming language
15 August 2017   2247

Swith statement is done by the case in Ruby. 

First example:

case n
when 0
  puts 'You typed zero'
when 1, 9
  puts 'n is a perfect square'
when 2
  puts 'n is a prime number'
  puts 'n is an even number'
when 3, 5, 7
  puts 'n is a prime number'
when 4, 6, 8
  puts 'n is an even number'
else
  puts 'Only single-digit numbers are allowed'
end

Second example:

score = 70

result = case score
   when 0..40 then "Fail"
   when 41..60 then "Pass"
   when 61..70 then "Pass with Merit"
   when 71..100 then "Pass with Distinction"
   else "Invalid Score"
end

You can also use case impression.

puts case a
when 1..5
  "It's between 1 and 5"
when 6
  "It's 6"
when String
  "You passed a string"
else
  "You gave me #{a} -- I have no idea what to do with that."
end

 

Ruby\RoR News Digest 30.06 - 5.07

Let's check what happened in Ruby programming this week and learn about latest updates and news
05 July 2019   1015

Greetings! I hope your week went great! Here's new Ruby/RoR news digest.

Articles and Guides

  • 6 changes you might have missed coming in Ruby 2.7

Learn about some tiny, but interest thing you really may not noted in this Ruby version.

  • Sorbetting a gem, or the story of the first adoption

As you may remember, new type checker for Ruby called Sorbet was released last week;  in this article, programmer shares his thoughts and impressions on the topic.

  • I just made Ruby++

The ++ implementing was more difficult. than expected.

  • Brewing our own Template Lexer in Ruby

A practical walkthrough of building a basic lexer (the parser part will come later) as a way to implement your own templating language

  • Rails 6 adds support for Multi Environment credentials

In Rails 6, different environments credintials can now be stored in separate files personal key for each.

  • In-App Payments Backend Using Ruby

Updates

  • HTTP Debugging for Ruby

Free version available too.