6.16. Ruby judgment

发布时间 :2024-01-16 01:55:15 UTC      

Ruby provides several common conditional structures. Here, we will explain all the conditional statements and the modifiers available in Ruby.

6.16.1. Ruby if...else statement #

Grammar #

ifconditional[then]code...[elsifconditional[then]code...]...[elsecode...]end

if expressions are used for conditional execution. Value false and nil if false, all other values are true. Note that Ruby uses the elsif , not using the else if and elif .

If conditional is true, execute code . If conditional is not true, then execute else clause specified in the code .

Usually we omit the reserved words. then . If you want to write a complete if type, you must use the then separate conditional and program blocks as follows:

ifa==4thena=7end

Example #

#!/usr/bin/ruby#-*- coding: UTF-8 -*-x=1ifx>2puts"x >
2"elsifx<=2andx!=0puts"X is 1 "elseputs" and cannot determine the value of x "end

The output result of the above example:

x is 1

6.16.2. Ruby if modifier #

Grammar #

codeifcondition

if modifier phrase indicates that when if will not be implemented until the condition on the right is established. if , the formula on the left. That is, if conditional is true, execute code .

Example #

#!/usr/bin/ruby$debug=1print"debug\\n"if$debug

The output result of the above example:

debug

6.16.3. Ruby unless statement #

Grammar #

unlessconditional[then]code[elsecode]end

unless formula sum if has the opposite effect, that is, if conditional is false, execute code . If conditional is true, execute else clause specified in the code .

Example #

#!/usr/bin/ruby#-*- coding: UTF-8 -*-x=1unlessx>2puts"x >
2"elseputs"x < 2"end

The output result of the above example is:

x < 2

6.16.4. Ruby unless modifier #

Grammar #

codeunlessconditional

If conditional is false, execute code .

Example #

#!/usr/bin/ruby#-*- coding: UTF-8 -*-$var=1print"1 --
This line outputs\\n"if$varprint"2 --
This line does not output\\n"unless$var$var=falseprint"3
-- This line outputs\\n"unless$var

The output result of the above example:

1 -- This line outputs
3 -- This line outputs

6.16.5. Ruby case statement #

Grammar #

caseexpression[whenexpression[,expression...][then]code]...[elsecode]end

case to one first expression the matching judgment is made, and then the branches are selected according to the matching results.

It uses the === operator comparison when specified expression .If it is consistent, then execute it when part of the content.

Usually we omit the reserved words then . If you want to write a complete when type, you must use the then separate conditional andprogram blocks. As follows:

whena==4thena=7end

Therefore:

caseexpr0whenexpr1,expr2stmt1whenexpr3,expr4stmt2elsestmt3end

Basically similar to:

\_tmp=expr0ifexpr1===\_tmp\|\|expr2===\_tmpstmt1elsifexpr3===\_tmp\|\|expr4===\_tmpstmt2elsestmt3end

Example #

#!/usr/bin/ruby#-*- coding: UTF-8
-*-$age=5case$agewhen0..2puts"baby"when3..
6puts"child"when7..12puts"child"when13..18puts"
youth"elseputs"Other age groups"end

The output result of the above example is:

child

When case the expression part of the is omitted, the first when expression whose conditional part is true.

foo=falsebar=truequu=falsecasewhenfoothenputs'foo is
true'whenbarthenputs'bar is true'whenquuthenputs'quu is true'end#display
"bar is true"
Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.