English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Ruby provides several very common conditional structures. Here, we will explain all the conditional statements and the modifiers available in Ruby.
if conditional [then] code... [elsif conditional [then] code...]... [else code...] end
if expression is used for conditional execution. Value false and nil False, all other values are true. Please note that Ruby uses elsif, not else if and elif.
if conditional If it is true, then execute code. If conditional is not true, then execute the else clause specified code.
Generally, we omit the keyword 'then'. If you want to write the complete 'if' statement on a single line, you must separate the condition from the code block with 'then'. As shown below:
if a == 4 then a = 7 end
!#/usr/bin/ruby # -*- coding: UTF-8 -*- x=1 if x > 2 puts "x is greater than 2" elsif x <= 2 and x!=0 puts "x is 1" else puts "Unable to determine the value of x" end
The output result of the above example is:
x is 1
code if condition
The 'if' modifier group indicates that the expression on the left of 'if' is executed only when the condition on the right of 'if' is true. That is, if conditional If it is true, then execute code.
!#/usr/bin/ruby $debug=1 print "debug\n" if $debug
The output result of the above example is:
debug
unless conditional [then] code [else code ] end
The 'unless' statement is the opposite of the 'if' statement, that is, if conditional If it is false, then execute code. If conditional If it is true, then execute the else clause specified code.
!#/usr/bin/ruby # -*- coding: UTF-8 -*- x=1 unless x>2 puts "x is less than 2" else puts "x is greater than 2" end
The output result of the above example is:
x is less than 2
code unless conditional
if conditional If it is false, then execute code.
!#/usr/bin/ruby # -*- coding: UTF-8 -*- $var = 1 print "1 -- This line outputs " if $var print "2 -- This line does not output " unless $var $var = false print "3 -- This line outputs " unless $var
The output result of the above example is:
1 -- This line outputs 3 -- This line outputs
case expression [when expression [, expression ...] [then] code ]... [else code ] end
case first checks a expression for matching judgment, and then performs branch selection based on the matching result.
It uses ===operator comparison when specified expression, if consistent, execute when Part of the content.
Generally, we omit the keyword 'then'. If you want to write the complete 'when' statement on a single line, you must separate the condition from the code block with 'then'. As shown below:
when a == 4 then a = 7 end
Therefore:
case expr0 when expr1, expr2 stmt1 when expr3, expr4 stmt2 else stmt3 end
Basically similar to:
_tmp = expr0 if expr1 === _tmp || expr2 === _tmp stmt1 elsif expr3 === _tmp || expr4 === _tmp stmt2 else stmt3 end
!#/usr/bin/ruby # -*- coding: UTF-8 -*- $age = 5 case $age when 0 .. 2 puts "Infant" when 3 .. 6 puts "Child" when 7 .. 12 puts "child" when 13 .. 18 puts "Youth" else puts "Other age groups" end
The output result of the above example is:
Child
When the 'expression' part of case is omitted, the expression of the first when condition that is true is calculated.
foo = false bar = true quu = false case when foo then puts 'foo is true' when bar then puts 'bar is true' when quu then puts 'quu is true' end # Display "bar is true"