Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Which statements are judged by Ruby?

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article, the editor introduces in detail "what are the Ruby judgment sentences", with detailed contents, clear steps and proper handling of details. I hope this article "what are the Ruby judgment sentences" can help you solve your doubts? let's follow the editor's ideas to learn new knowledge.

Ruby if... Else statement

Grammar

If conditional [then] code... [elsif conditional [then] code...]... [else code...] end

If expressions are used for conditional execution. The values false and nil are false, and all other values are true. Note that Ruby uses elsif, not else if and elif. If conditional is true, code is executed. If conditional is not true, the code specified in the else clause is executed. Usually we omit the reserved word then. If you want to write a complete if in one line, you must separate conditional and program blocks with then. As follows:

If a = = 4 then a = 7 end

Example

#! / usr/bin/ruby#-*-coding: UTF-8-*-x=1if x > 2 puts "x greater than 2" elsif x "x is 1" else puts "cannot know the value of x" end

The output result of the above example:

X is the 1Ruby if modifier

Grammar

Code if condition

The if modifier phrase means that the expression on the left side of if is executed only when the condition on the right side of if is true. That is, if conditional is true, code is executed.

Example

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

The output result of the above example:

DebugRuby unless statement

Grammar

Unless conditional [then] code [else code] end

Unless and if have the opposite effect, that is, if conditional is false, code is executed. If conditional is true, the code specified in the else clause is executed.

Example

#! / usr/bin/ruby#-*-coding: UTF-8-*-x=1unless x > 2 puts "x less than 2" else puts "x greater than 2" end

The output result of the above example is:

X is less than the 2Ruby unless modifier

Grammar

Code unless conditional

If conditional is false, code is executed.

Example

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

The output result of the above example:

1-- this line outputs 3-- this line outputs Ruby case statements

Grammar

Case expression [when expression [, expression...] [then] code]... [else code] end

Case first determines the matching of an expression, and then selects branches according to the matching results. It uses the = = operator to compare the expression specified by when and, if consistent, executes the contents of the when section. Usually we omit the reserved word then. If you want to write a complete when in one line, you must separate conditional and program blocks with then. As follows:

When a = = 4 then a = 7 end

Therefore:

Case expr0when expr1, expr2 stmt1when expr3, expr4 stmt2else stmt3end

Basically similar to:

_ tmp = expr0if expr1 = = _ tmp | | expr2 = = _ tmp stmt1elsif expr3 = _ tmp | | expr4 = _ tmp stmt2else stmt3end

Example

#! / usr/bin/ruby#-*-coding: UTF-8-*-$age = 5case $agewhen 0. 2 puts "baby" when 3. 6 puts "kid" when 7.. 12 puts "child" when 13.. 18 puts "teenager" else puts "end" of other age groups

The output result of the above example is:

Child

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

Foo = falsebar = truequu = falsecasewhen foo then puts' foo is true'when bar then puts' bar is true'when quu then puts' quu is true'end# shows that "bar is true" has been read here, and the article "what are the sentences of Ruby judgment" has been introduced. If you want to master the knowledge points of this article, you need to practice and use it before you can understand it. If you want to know more about related articles, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report