📄 2.8.htm
字号:
width=14 align=textTop border=0></A> <A class=v2
title="Printer-friendly version of this section"
href="http://proquest.safaribooksonline.com/JVXSL.asp?x=1&mode=print&sortKey=title&sortOrder=asc&view=&xmlid=0-59600-214-9/ruby-CHP-2-SECT-8&open=false&catid=&s=1&b=1&f=1&t=1&c=1&u=1"
target=_new>Print</A> <A
href="http://proquest.safaribooksonline.com/JVXSL.asp?x=1&mode=email&sortKey=title&sortOrder=asc&view=&xmlid=0-59600-214-9/ruby-CHP-2-SECT-8&open=false&catid=&s=1&b=1&f=1&t=1&c=1&u=1"><IMG
height=9 hspace=3 src="2.8.files/img_email.gif" width=15
align=absMiddle border=0></A> <A class=v2
title="E-Mail this section"
href="http://proquest.safaribooksonline.com/JVXSL.asp?x=1&mode=email&sortKey=title&sortOrder=asc&view=&xmlid=0-59600-214-9/ruby-CHP-2-SECT-8&open=false&catid=&s=1&b=1&f=1&t=1&c=1&u=1">E-Mail</A>
<A
href="http://proquest.safaribooksonline.com/JVXSL.asp?x=1&mode=bookmark&sortKey=title&sortOrder=asc&view=&xmlid=0-59600-214-9/ruby-CHP-2-SECT-8&open=false&catid=&s=1&b=1&f=1&t=1&c=1&u=1"><IMG
height=14 hspace=3 src="2.8.files/img_bookmarks.gif" width=6
align=absMiddle border=0></A> <A class=v2
title="Add a bookmark about this section"
href="http://proquest.safaribooksonline.com/JVXSL.asp?x=1&mode=bookmark&sortKey=title&sortOrder=asc&view=&xmlid=0-59600-214-9/ruby-CHP-2-SECT-8&open=false&catid=&s=1&b=1&f=1&t=1&c=1&u=1">Add
Bookmark</A></TD>
<TD class=v2 vAlign=top align=right> <A
href="http://proquest.safaribooksonline.com/JVXSL.asp?x=1&mode=section&sortKey=title&sortOrder=asc&view=&xmlid=0-59600-214-9/ruby-CHP-2-SECT-7&open=false&catid=&s=1&b=1&f=1&t=1&c=1&u=1"><IMG
height=16 src="2.8.files/btn_prev.gif" width=56
align=absMiddle border=0></A> <A
href="http://proquest.safaribooksonline.com/JVXSL.asp?x=1&mode=section&sortKey=title&sortOrder=asc&view=&xmlid=0-59600-214-9/ruby-CHP-2-SECT-9&open=false&catid=&s=1&b=1&f=1&t=1&c=1&u=1"><IMG
height=16 src="2.8.files/btn_next.gif" width=41
align=absMiddle border=0></A></TD></TR></TBODY></TABLE>
<HR SIZE=1>
<TABLE cellSpacing=0 cellPadding=5 width="100%" border=0>
<TBODY>
<TR>
<TD vAlign=top width=67 rowSpan=3><IMG
src="2.8.files/ruby_xs.gif" width=67 border=0></TD>
<TD class=v2 vAlign=top><A class=v1
href="http://proquest.safaribooksonline.com/JVXSL.asp?x=1&mode=section&sortKey=title&sortOrder=asc&view=&xmlid=0-59600-214-9&open=false&catid=&s=1&b=1&f=1&t=1&c=1&u=1&srchText=">Ruby
in a Nutshell</A><BR>By Yukihiro Matsumoto<BR></TD></TR>
<TR>
<TD class=v2> </TD></TR>
<TR>
<TD class=t1 vAlign=bottom>Chapter 2. Language
Basics</TD></TR></TBODY></TABLE>
<HR SIZE=1>
<BR>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD vAlign=top><A name=ruby-CHP-2-SECT-8></A>
<H3 class=docSection1Title>2.8 Control Structures</H3><A
name=ruby-IDXTERM-278></A>
<P class=docText>Ruby offers control structures that are
pretty common to modern languages, but it also has a few
unique ones. </P><A name=ch02-62-fm2xml></A><A
name=IXT-2-127037></A><A name=IXT-2-127038></A><A
name=IXT-2-127039></A><A name=IXT-2-127040></A><A
name=ruby-IDXTERM-283></A>
<TABLE cellPadding=5 width=515 border=0>
<TBODY>
<TR>
<TD align=left><B><I>if Statement</I></B></TD>
<TD align=right></TD></TR></TBODY></TABLE>
<HR align=left width=515 color=black noShade SIZE=3>
<TABLE cellPadding=5 width=515 border=0>
<TBODY>
<TR>
<TD align=left><PRE>if <TT><I>conditional</I></TT> [then]
<TT><I>code</I></TT>
[elsif <TT><I>conditional</I></TT> [then]
<TT><I>code</I></TT>]...
[else
<TT><I>code</I></TT>]
end</PRE></TD>
<TD align=right></TD></TR></TBODY></TABLE>
<P class=docText><A name=IXT-2-127038></A><A
name=IXT-2-127039></A><A name=IXT-2-127040></A><A
name=ruby-IDXTERM-283></A>Executes <TT><I>code</I></TT> if the
<TT><I>conditional</I></TT> is <TT>true</TT>. True is
interpreted as anything that isn't <TT>false</TT> or
<TT>nil</TT>. If the <TT><I>conditional</I></TT> isn't
<TT>true</TT>, <TT><I>code</I></TT> specified in the
<TT>else</TT> clause is executed. An <TT>if</TT> expression's
<TT><I>conditional</I></TT> is separated from
<TT><I>code</I></TT> by the reserved word <TT>then</TT>, a
newline, or a semicolon. The reserved word <TT>if</TT> can be
used as a statement modifier.</P><PRE><TT><I>code</I></TT> if <TT><I>conditional</I></TT></PRE>
<P class=docText>Executes <TT>code</TT> if
<TT>conditional</TT> is <TT>true</TT>.</P><A
name=ch02-63-fm2xml></A><A name=IXT-2-127041></A>
<TABLE cellPadding=5 width=515 border=0>
<TBODY>
<TR>
<TD align=left><B><I>unless Statement</I></B></TD>
<TD align=right></TD></TR></TBODY></TABLE>
<HR align=left width=515 color=black noShade SIZE=3>
<TABLE cellPadding=5 width=515 border=0>
<TBODY>
<TR>
<TD align=left><PRE>unless <TT><I>conditional</I></TT> [then]
<TT><I>code</I></TT>
[else
<TT><I>code</I></TT>]
end</PRE></TD>
<TD align=right></TD></TR></TBODY></TABLE>
<P class=docText>Executes code if <TT><I>conditional</I></TT>
is <TT>false</TT>. If the <TT><I>conditional</I></TT> is
<TT>true</TT>, <TT><I>code</I></TT> specified in the
<TT>else</TT> clause is executed. Like <TT>if</TT>,
<TT>unless</TT> can be used as a statement modifier. <PRE><TT><I>code</I></TT> unless <TT><I>conditional</I></TT></PRE>Executes
<TT><I>code</I></TT> unless <TT><I>conditional</I></TT> is
<TT>true</TT>.
<P></P><A name=ch02-64-fm2xml></A><A name=IXT-2-127042></A><A
name=IXT-2-127043></A><A name=IXT-2-127044></A><A
name=IXT-2-127045></A><A name=IXT-2-127046></A><A
name=IXT-2-127047></A><A name=IXT-2-127048></A>
<TABLE cellPadding=5 width=515 border=0>
<TBODY>
<TR>
<TD align=left><B><I>case Statement</I></B></TD>
<TD align=right></TD></TR></TBODY></TABLE>
<HR align=left width=515 color=black noShade SIZE=3>
<TABLE cellPadding=5 width=515 border=0>
<TBODY>
<TR>
<TD align=left><PRE>case <TT><I>expression</I></TT>
[when <TT><I>expression</I></TT>[, <TT><I>expression</I></TT>...] [then]
<TT><I>code</I></TT>]...
[else
<TT><I>code</I></TT>]
end</PRE></TD>
<TD align=right></TD></TR></TBODY></TABLE>
<P class=docText><A name=IXT-2-127043></A><A
name=IXT-2-127044></A><A name=IXT-2-127045></A><A
name=IXT-2-127046></A><A name=IXT-2-127047></A><A
name=IXT-2-127048></A>Compares the <TT><I>expression</I></TT>
specified by <TT>case</TT> and that specified by <TT>when</TT>
using the <TT>===</TT> operator and executes the
<TT><I>code</I></TT> of the <TT>when</TT> clause that matches.
The <TT><I>expression</I></TT> specified by the <TT>when</TT>
clause is evaluated as the left operand. If no <TT>when</TT>
clauses match, <TT>case</TT> executes the <TT><I>code</I></TT>
of the <TT>else</TT> clause. A <TT>when</TT> statement's
<TT><I>expression</I></TT> is separated from
<TT><I>code</I></TT> by the reserved word <TT>then</TT>, a
newline, or a semicolon. </P><A name=ch02-65-fm2xml></A><A
name=IXT-2-127049></A><A name=IXT-2-127050></A><A
name=IXT-2-127051></A><A name=IXT-2-127052></A><A
name=IXT-2-127053></A><A name=IXT-2-127054></A><A
name=IXT-2-127055></A>
<TABLE cellPadding=5 width=515 border=0>
<TBODY>
<TR>
<TD align=left><B><I>while Statement</I></B></TD>
<TD align=right></TD></TR></TBODY></TABLE>
<HR align=left width=515 color=black noShade SIZE=3>
<TABLE cellPadding=5 width=515 border=0>
<TBODY>
<TR>
<TD align=left><PRE>while <TT><I>conditional</I></TT> [do]
<TT><I>code</I></TT>
end</PRE></TD>
<TD align=right></TD></TR></TBODY></TABLE>
<P class=docText><A name=IXT-2-127050></A><A
name=IXT-2-127051></A><A name=IXT-2-127052></A><A
name=IXT-2-127053></A>Executes <TT><I>code</I></TT> while
<TT><I>conditional</I></TT> is <TT>true</TT>. A <TT>while</TT>
loop's <TT><I>conditional</I></TT> is separated from
<TT><I>code</I></TT> by the reserved word <TT>do</TT>, a
newline,\, or a semicolon. The reserved word <TT>while</TT>
can be used as statement modifier.</P><PRE><TT><I>code</I></TT> while <TT><I>conditional</I></TT></PRE>
<P class=docText>Executes<TT><I>code</I></TT> while
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -