📄 2.5.htm
字号:
src="2.5.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-5&open=false&catid=&s=1&b=1&f=1&t=1&c=1&u=1"
target=_new><IMG height=12 src="2.5.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-5&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-5&open=false&catid=&s=1&b=1&f=1&t=1&c=1&u=1"><IMG
height=9 hspace=3 src="2.5.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-5&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-5&open=false&catid=&s=1&b=1&f=1&t=1&c=1&u=1"><IMG
height=14 hspace=3 src="2.5.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-5&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-4&open=false&catid=&s=1&b=1&f=1&t=1&c=1&u=1"><IMG
height=16 src="2.5.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-6&open=false&catid=&s=1&b=1&f=1&t=1&c=1&u=1"><IMG
height=16 src="2.5.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.5.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-5></A>
<H3 class=docSection1Title>2.5 Variables</H3><A
name=ruby-IDXTERM-197></A>
<P class=docText>There are five types of variables in Ruby:
global, instance, class, locals and constants. As you might
expect, global variables are accessible globally to the
program, instance variables belong to an object, class
variables to a class and constants are, well... constant. Ruby
uses special characters to differentiate between the different
kinds of variables. At a glance, you can tell what kind of
variable is being used. </P><A name=ch02-30-fm2xml></A><A
name=IXT-2-126964></A><A name=IXT-2-126965></A><A
name=IXT-2-126966></A><A name=IXT-2-126967></A>
<TABLE cellPadding=5 width=515 border=0>
<TBODY>
<TR>
<TD align=left><B><I>Global 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><PRE>$foo</PRE></TD>
<TD align=right></TD></TR></TBODY></TABLE>
<P class=docText><A name=IXT-2-126965></A><A
name=IXT-2-126966></A><A name=IXT-2-126967></A>Global
variables begin with <TT>$</TT>. Uninitialized global
variables have the value <TT>nil</TT> (and produce warnings
with the <TT>-w</TT> option). Some global variables have
special behavior. See <A class=docLink
href="http://proquest.safaribooksonline.com/JVXSL.asp?xmlid=0-59600-214-9/ruby-CHP-3-SECT-1#ruby-CHP-3-SECT-1">Section
3.1</A> in <A class=docLink
href="http://proquest.safaribooksonline.com/JVXSL.asp?xmlid=0-59600-214-9/ruby-CHP-3#ruby-CHP-3">Chapter
3</A>. </P><A name=ch02-31-fm2xml></A><A
name=IXT-2-126968></A><A name=IXT-2-126969></A>
<TABLE cellPadding=5 width=515 border=0>
<TBODY>
<TR>
<TD align=left><B><I>Instance 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><PRE>@foo</PRE></TD>
<TD align=right></TD></TR></TBODY></TABLE>
<P class=docText><A name=IXT-2-126969></A>Instance variables
begin with <TT>@</TT>. Uninitialized instance variables have
the value <TT>nil</TT> (and produce warnings with the
<TT>-w</TT> option). </P><A name=ch02-32-fm2xml></A><A
name=IXT-2-126970></A><A name=IXT-2-126971></A>
<TABLE cellPadding=5 width=515 border=0>
<TBODY>
<TR>
<TD align=left><B><I>Class 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><PRE>@@foo</PRE></TD>
<TD align=right></TD></TR></TBODY></TABLE>
<P class=docText><A name=IXT-2-126971></A>Class variables
begin with <TT>@@</TT> and must be initialized before they can
be used in method definitions. Referencing an uninitialized
class variable produces an error. Class variables are shared
among descendants of the class or module in which the class
variables are defined. Overriding class variables produce
warnings with the -<TT>w</TT> option. </P><A
name=ch02-33-fm2xml></A><A name=IXT-2-126972></A><A
name=IXT-2-126973></A><A name=IXT-2-126974></A><A
name=IXT-2-126975></A>
<TABLE cellPadding=5 width=515 border=0>
<TBODY>
<TR>
<TD align=left><B><I>Local 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><PRE>foo</PRE></TD>
<TD align=right></TD></TR></TBODY></TABLE>
<P class=docText><A name=IXT-2-126972></A><A
name=IXT-2-126973></A><A name=IXT-2-126974></A><A
name=IXT-2-126975></A>Local variables begin with a lowercase
letter or <TT>_</TT>. The scope of a local variable ranges
from <TT>class</TT>, <TT>module</TT>, <TT>def</TT>, or
<TT>do</TT> to the corresponding <TT>end</TT> or from a
block's opening brace to its close brace <TT>{}</TT>. The
scope introduced by a block allows it to reference local
variables outside the block, but scopes introduced by others
don't. When an uninitialized local variable is referenced, it
is interpreted as a call to a method that has no arguments.
</P><A name=ch02-34-fm2xml></A><A name=IXT-2-126976></A><A
name=IXT-2-126977></A><A name=IXT-2-126978></A>
<TABLE cellPadding=5 width=515 border=0>
<TBODY>
<TR>
<TD align=left><B><I>Constants</I></B></TD>
<TD align=right></TD></TR></TBODY></TABLE>
<HR align=left width=515 color=black noShade SIZE=3>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -