📄 2.6.htm
字号:
href="http://proquest.safaribooksonline.com/JVXSL.asp?mode=findbook&view=isbn">ISBN</A></TD></TR>
<TR>
<TD class=v2 height=18>• <A class=v2
title="Search for a book with a particular title"
href="http://proquest.safaribooksonline.com/JVXSL.asp?mode=findbook&view=booktitle">Title</A></TD></TR>
<TR>
<TD class=v2 height=18>• <A class=v2
title="Search for all books by a particular publisher"
href="http://proquest.safaribooksonline.com/JVXSL.asp?mode=findbook&view=publ">Publisher</A></TD></TR>
<TR>
<TD class=v1 height=5></TD></TR></TBODY></TABLE></TD>
<TD align=right width=1 rowSpan=3><IMG height=1
src="2.6.files/spacer.gif" width=1 border=0></TD></TR>
<TR>
<TD class=v1 align=middle width=188 height=1><IMG height=1
src="2.6.files/spacer.gif" width=1
border=0></TD></TR></TBODY></TABLE></DIV></FORM></TD>
<TD align=middle>
<TABLE width="95%">
<TBODY>
<TR>
<TD class=v2 align=middle>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD height=15> </TD></TR>
<TR>
<TD class=v2 vAlign=top><A
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-6&open=false&catid=&s=1&b=1&f=1&t=1&c=1&u=1"
target=_new><IMG height=12 src="2.6.files/img_print.gif"
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-6&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-6&open=false&catid=&s=1&b=1&f=1&t=1&c=1&u=1"><IMG
height=9 hspace=3 src="2.6.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-6&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-6&open=false&catid=&s=1&b=1&f=1&t=1&c=1&u=1"><IMG
height=14 hspace=3 src="2.6.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-6&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-5&open=false&catid=&s=1&b=1&f=1&t=1&c=1&u=1"><IMG
height=16 src="2.6.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-7&open=false&catid=&s=1&b=1&f=1&t=1&c=1&u=1"><IMG
height=16 src="2.6.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.6.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-6></A>
<H3 class=docSection1Title>2.6 Operators</H3><A
name=ruby-IDXTERM-233></A>
<P class=docText>Ruby supports a rich set of operators, as
you'd expect from a modern language. However, in keeping with
Ruby's object-oriented nature, most operators are in fact
method calls. This flexibility allows you to change the
semantics of these operators wherever it might make sense.
</P><A name=ruby-CHP-2-SECT-6.1></A>
<H4 class=docSection2Title>2.6.1 Operator Expressions</H4>
<P class=docText>Most operators are actually method calls. For
example, <TT>a + b</TT> is interpreted as <TT>a.+(b)</TT>,
where the <TT>+</TT> method in the object referred to by
variable <TT>a</TT> is called with <TT>b</TT> as its argument.
</P>
<P class=docText>For each operator (<TT>+</TT> <TT>-</TT>
<TT>*</TT> <TT>/</TT> <TT>%</TT> <TT>**</TT> <TT>&</TT>
<TT>|</TT> <TT>^</TT> <TT><<</TT> <TT>>></TT>
<TT>&&</TT> <TT>||</TT>), there is a corresponding
form of abbreviated assignment operator (<TT>+=</TT>
<TT>-=</TT> etc.) </P>
<P class=docText>Here are the operators shown in order of
precedence (highest to lowest)<TT>:</TT> </P>
<DL class=docList>
<DT><TT>::</TT>
<DT><TT>[]</TT>
<DT><TT>**</TT>
<DT><TT>+(unary) -(unary) ! ~</TT>
<DT><TT>* / %</TT>
<DT><TT>+ -</TT>
<DT><TT><< >></TT>
<DT><TT>&</TT>
<DT><TT>| ^</TT>
<DT><TT>> >= < <=</TT>
<DT><TT><=> == === != =~ !~</TT>
<DT><TT>&&</TT>
<DT><TT>||</TT>
<DT><TT>.. ...</TT>
<DT><TT>?:</TT>
<DT><TT>=</TT> (and abbreviated assignment operators such as
+=, -=, etc.)
<DT><TT>not</TT>
<DT><TT>and or</TT> </DT></DL><A
name=ruby-CHP-2-SECT-6.1.1></A>
<H5 class=docSection3Title>2.6.1.1 Nonmethod operators</H5>
<P class=docText>The following operators aren't methods and,
therefore, can't be redefined: </P>
<DL class=docList>
<DT><TT>...</TT>
<DT><TT>!</TT>
<DT><TT>not</TT>
<DT><TT>&&</TT>
<DT><TT>and</TT>
<DT><TT>||</TT>
<DT><TT>or</TT>
<DT><TT>::</TT>
<DT><TT>=</TT>
<DT><TT>+=</TT>, <TT>-=</TT>, (and other abbreviated
assignment operators)
<DT><TT>? :</TT> (ternary operator) </DT></DL><A
name=ruby-CHP-2-SECT-6.1.2></A>
<H5 class=docSection3Title>2.6.1.2 Range operators</H5><A
name=IXT-2-126996></A>
<P class=docText><A name=IXT-2-126997></A><A
name=IXT-2-126998></A>Range operators function differently
depending on whether or not they appear in conditionals,
<TT>if</TT> expressions, and <TT>while</TT> loops. </P>
<P class=docText><A name=IXT-2-126999></A>In conditionals,
they return <TT>true</TT> from the point right operand is
<TT>true</TT> until left operand is <TT>true:</TT> </P>
<DL class=docList>
<DT><SPAN class=docPubcolor><TT><I>expr1</I></TT> <SPAN
class=docPubcolor><SPAN class=docMonofont>..</SPAN></SPAN>
<TT><I>expr2</I></TT></SPAN>
<DD>
<P class=docList>Evaluates <TT><I>expr2</I></TT> immediately
after <TT><I>expr1</I></TT> turns <TT>true</TT>. </P>
<DT><SPAN class=docPubcolor><TT><I>expr1</I></TT> <SPAN
class=docPubcolor><SPAN class=docMonofont>...</SPAN></SPAN>
<TT><I>expr2</I></TT></SPAN>
<DD>
<P class=docList>Evaluates <TT><I>expr2</I></TT> on the
iteration after <TT><I>expr1</I></TT> turns <TT>true</TT>.
</P></DD></DL>
<P class=docText>In other contexts, they create a range
object:</P>
<DL class=docList>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -