📄 2.5.htm
字号:
<TABLE cellPadding=5 width=515 border=0>
<TBODY>
<TR>
<TD align=left><PRE>Foo</PRE></TD>
<TD align=right></TD></TR></TBODY></TABLE>
<P class=docText><A name=IXT-2-126977></A><A
name=IXT-2-126978></A>Constants begin with an uppercase
letter. Constants defined within a class or module can be
accessed from within that class or module, and those defined
outside a class or module can be accessed globally. Constants
may not be defined within methods. Referencing an
uninitialized constant produces an error. Making an assignment
to a constant that is already initialized produces a warning,
not an error. You may feel it contradicts the name "constant,"
but remember, this is listed under "variables." </P><A
name=ch02-35-fm2xml></A><A name=IXT-2-126979></A><A
name=ruby-IDXTERM-214></A><A name=IXT-2-126980></A><A
name=IXT-2-126981></A><A name=IXT-2-126982></A><A
name=IXT-2-126983></A><A name=IXT-2-126984></A><A
name=IXT-2-126985></A><A name=IXT-2-126986></A><A
name=IXT-2-126987></A>
<TABLE cellPadding=5 width=515 border=0>
<TBODY>
<TR>
<TD align=left><B><I>Pseudo-Variables</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></TD>
<TD align=right></TD></TR></TBODY></TABLE>
<P class=docText><A name=IXT-2-126981></A>In addition to the
variables discussed, there are also a few <SPAN
class=docEmphasis>pseudo-variables</SPAN>. Pseudo-variables
have the appearance of local variables but behave like
constants. Assignments may not be made to pseudo-variables.
</P><A name=IXT-2-126982></A><A name=IXT-2-126983></A><A
name=IXT-2-126984></A><A name=IXT-2-126985></A><A
name=IXT-2-126986></A><A name=IXT-2-126987></A>
<DL class=docList><A name=IXT-2-126982></A>
<DT><SPAN class=docPubcolor><SPAN class=docPubcolor><SPAN
class=docMonofont>self</SPAN></SPAN></SPAN>
<DD>
<P class=docList>The receiver object of the current
method</P><A name=IXT-2-126983></A>
<DT><SPAN class=docPubcolor><SPAN class=docPubcolor><SPAN
class=docMonofont>true</SPAN></SPAN></SPAN>
<DD>
<P class=docList>Value representing <TT>true</TT></P><A
name=IXT-2-126984></A>
<DT><SPAN class=docPubcolor><SPAN class=docPubcolor><SPAN
class=docMonofont>false</SPAN></SPAN></SPAN>
<DD>
<P class=docList>Value representing <TT>false</TT></P><A
name=IXT-2-126985></A>
<DT><SPAN class=docPubcolor><SPAN class=docPubcolor><SPAN
class=docMonofont>nil</SPAN></SPAN></SPAN>
<DD>
<P class=docList>Value representing "undefined"; interpreted
as <TT>false</TT> in conditionals </P><A
name=IXT-2-126986></A>
<DT><SPAN class=docPubcolor>__<SPAN class=docPubcolor><SPAN
class=docMonofont>FILE</SPAN></SPAN>__</SPAN>
<DD>
<P class=docList>The name of the current source file</P><A
name=IXT-2-126987></A>
<DT><SPAN class=docPubcolor>__<SPAN class=docPubcolor><SPAN
class=docMonofont>LINE</SPAN></SPAN>__</SPAN>
<DD>
<P class=docList>The current line number in the source
file</P></DD></DL><A name=ch02-36-fm2xml></A><A
name=IXT-2-126988></A><A name=IXT-2-126989></A><A
name=IXT-2-126990></A><A name=IXT-2-126991></A>
<TABLE cellPadding=5 width=515 border=0>
<TBODY>
<TR>
<TD align=left><B><I>Assignment</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><TT><I>target</I></TT> = <TT><I>expr</I></TT></PRE></TD>
<TD align=right></TD></TR></TBODY></TABLE>
<P class=docText>The following elements may assign targets:
</P><A name=IXT-2-126988></A><A name=IXT-2-126989></A><A
name=IXT-2-126990></A><A name=IXT-2-126991></A>
<DL class=docList><A name=IXT-2-126988></A>
<DT><I><SPAN class=docPubcolor>Global variables</SPAN></I>
<DD>
<P class=docList>Assignment to global variables alters
global status. It isn't recommended to use (or abuse) global
variables. They make programs cryptic. </P><A
name=IXT-2-126989></A>
<DT><I><SPAN class=docPubcolor>Local variables</SPAN></I>
<DD>
<P class=docList>Assignment to uninitialized local variables
also serves as variable declaration. The variables start to
exist until the end of the current scope is reached. The
lifetime of local variables is determined when Ruby parses
the program. </P><A name=IXT-2-126990></A>
<DT><I><SPAN class=docPubcolor>Constants</SPAN></I>
<DD>
<P class=docList>Assignment to constants may not appear
within a method body. In Ruby, re-assignment to constants
isn't prohibited, but it does raise a warning. </P><A
name=IXT-2-126991></A>
<DT><I><SPAN class=docPubcolor>Attributes</SPAN></I>
<DD>
<P class=docList>Attributes take the following form:</P><PRE><TT><I>expr.attr</I></TT></PRE>
<P class=docList>Assignment to attributes calls the
<TT><I>attr</I></TT><TT>=</TT> method of the result of
<TT><I>expr</I></TT>. </P>
<DT><I><SPAN class=docPubcolor>Elements</SPAN></I>
<DD>
<P class=docList>Elements take the following form:</P><PRE><TT><I>expr</I></TT>[<TT><I>arg</I></TT>...]</PRE>
<P class=docList>Assignment to elements calls the
<TT>[]=</TT> method of the result of <TT><I>expr</I></TT>.
</P></DD></DL><A name=ch02-37-fm2xml></A><A
name=IXT-2-126992></A>
<TABLE cellPadding=5 width=515 border=0>
<TBODY>
<TR>
<TD align=left><B><I>Parallel Assignment</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><TT><I>target</I></TT>[, <TT><I>target</I></TT>...][, *<TT><I>target</I></TT>] = <TT><I>expr</I></TT>[, <TT><I>expr</I></TT>...][, *<TT><I>expr</I></TT>]</PRE></TD>
<TD align=right></TD></TR></TBODY></TABLE>
<P class=docText><A name=IXT-2-126992></A>Targets on the left
side receive assignment from their corresponding expressions
on the right side. If the last left-side target is preceded by
<TT>*</TT>, all remaining right-side values are assigned to
the target as an array. If the last right-side expression is
preceded by <TT>*</TT>, the array elements of expression are
expanded in place before assignment. </P>
<P class=docText>If there is no corresponding expression,
<TT>nil</TT> is assigned to the target. If there is no
corresponding target, the value of right-side expression is
just ignored. </P><A name=ch02-38-fm2xml></A><A
name=IXTR3-2></A><A name=IXT-2-126993></A><A
name=IXT-2-126994></A><A name=IXT-2-126995></A><A
name=IXTR3-3></A>
<TABLE cellPadding=5 width=515 border=0>
<TBODY>
<TR>
<TD align=left><B><I>Abbreviated Assignment</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><TT><I>target</I></TT> <TT><I>op</I></TT>= <TT><I>expr</I></TT></PRE></TD>
<TD align=right></TD></TR></TBODY></TABLE>
<P class=docText>This is the abbreviated form of: </P><PRE><TT><I>target = target op expr</I></TT></PRE>
<P class=docText><A name=IXT-2-126993></A><A
name=IXT-2-126994></A><A name=IXT-2-126995></A>The following
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -