Switch statement in Ruby

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

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

 

Git 2.20 to be Available

Let's check updates and features of new version control system
11 December 2018   130

Distributed version control system Git has received another update. In order for Git 2.20 to appear, 83 developers made changes 962 to the zcode. According to the team, this is an order of magnitude higher than the same figure in the largest release of the 2.x.x branch.

The git branch -l command is now a shortened version of the git branch --list - it used to help run reflog during the creation of a new branch. Developers have limited the launch of git fetch: it is only possible with an indication of --force, to avoid problems with consistency when updating the link.

The git help -a and git help -av commands to help newbies display a more verbose output. To return to the old view, just type git help - no-verbose -a. In git send-email, it is possible to extract lines with addresses that end with “-by” from signatures. This is an incompatible change, and it can be disabled by adding to the --suppress-cc = misc-by command.

  • If the repository contains files whose addresses differ only in the case of letters, a warning will be displayed during the execution of git clone.
  • The git format-patch command received the --interdiff and --range-diff options, which in a note or comment list the differences between the existing and previous versions.
  • git mailinfo learned how to recover code patches sent by email with plain text and damaged due to hyphenation.
  • git multi-pack-index now fixes damage in .midx files.
  • Creating experimental commit-graph files for large repositories takes a lot of time, so the developers have provided a form of output about the state of the process.

Performance and Development Support

  • For working builds, the -Wunused-function compilation option is provided.
  • git submodule update is completely rewritten in C.
  • One of the continuous integration (CI) tests, designed to work with the unusual/experimental/random settings, now supports midx and commit-graph files.
  • A new mechanism for finding objects among a large number of pack-files. It relies on combining all .idx files into one.

The previous version of the system was released in September 2018.