📄 2.9.htm
字号:
<TR>
<TD align=left><B><I>module 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>module <TT><I>module_name</I></TT>
<TT><I> code</I></TT>
end</PRE></TD>
<TD align=right></TD></TR></TBODY></TABLE>
<P class=docText><A name=IXT-2-127105></A>A <TT>module</TT>
statement defines a module. <TT><I>module_name</I></TT> must
be a constant. The defined module is assigned to that
constant. If a module of the same name already exists, the
features of the new module definition are added to the
existing module. <TT>module</TT> statements introduce a new
scope for local variables. </P><A
name=ruby-CHP-2-SECT-9.5></A>
<H4 class=docSection2Title>2.9.5 Mix-ins</H4>
<P class=docText><A name=IXT-2-127106></A><A
name=IXT-2-127107></A><A name=IXT-2-127108></A>Properties
(methods and constants) defined by a module can be added to a
class or another module with the <TT>include</TT> method. They
can also be added to a specific object using the extend
method. See <TT>Module#include</TT> in <A class=docLink
href="http://proquest.safaribooksonline.com/JVXSL.asp?xmlid=0-59600-214-9/ruby-CHP-3-SECT-4#ruby-CHP-3-SECT-4.9">Section
3.4.9</A>, and the <TT>Object#extend</TT> in <A class=docLink
href="http://proquest.safaribooksonline.com/JVXSL.asp?xmlid=0-59600-214-9/ruby-CHP-3-SECT-4#ruby-CHP-3-SECT-4.1">Section
3.4.1</A>. </P><A name=ruby-CHP-2-SECT-9.6></A>
<H4 class=docSection2Title>2.9.6 Method Visibility</H4><A
name=IXT-2-127109></A>
<P class=docText>There are three types of method
visibility:</P><A name=IXT-2-127110></A><A
name=IXT-2-127111></A><A name=IXT-2-127112></A>
<DL class=docList><A name=IXT-2-127110></A>
<DT><SPAN class=docPubcolor><SPAN class=docPubcolor><SPAN
class=docMonofont>Public</SPAN></SPAN></SPAN>
<DD>
<P class=docList>Callable from anywhere</P><A
name=IXT-2-127111></A>
<DT><SPAN class=docPubcolor><SPAN class=docPubcolor><SPAN
class=docMonofont>Protected</SPAN></SPAN></SPAN>
<DD>
<P class=docList>Callable only from instances of the same
class</P><A name=IXT-2-127112></A>
<DT><SPAN class=docPubcolor><SPAN class=docPubcolor><SPAN
class=docMonofont>Private</SPAN></SPAN></SPAN>
<DD>
<P class=docList>Callable only in functional form (i.e.,
without the receiver specified) </P></DD></DL>
<P class=docText>Method visibility is defined using the
<TT>public</TT>, <TT>private</TT>, and <TT>protected</TT>
methods in classes and modules. </P>
<DL class=docList>
<DT><SPAN class=docPubcolor><SPAN class=docPubcolor><SPAN
class=docMonofont>public(</SPAN></SPAN>
<TT><I>[</I></TT><SPAN class=docPubcolor><SPAN
class=docMonofont>symbol</SPAN></SPAN>
<TT><I>...])</I></TT></SPAN>
<DD>
<P class=docList>Makes the method specified by
<TT>symbol</TT> public. The method must have been previously
defined. If no arguments are specified, the visibility of
all subsequently defined methods in the class or module is
made public. </P>
<DT><SPAN class=docPubcolor><SPAN class=docPubcolor><SPAN
class=docMonofont>protected([</SPAN></SPAN>
<TT><I>symbol</I></TT><SPAN class=docPubcolor><SPAN
class=docMonofont>...])</SPAN></SPAN> </SPAN>
<DD>
<P class=docList>Makes the method specified by
<TT>symbol</TT> protected. The method must have been
previously defined. If no arguments are specified, the
visibility of all subsequently defined methods in the class
or module is made protected. </P>
<DT><SPAN class=docPubcolor><SPAN class=docPubcolor><SPAN
class=docMonofont>private([</SPAN></SPAN>
<TT><I>symbol</I></TT><SPAN class=docPubcolor><SPAN
class=docMonofont>...])</SPAN></SPAN> </SPAN>
<DD>
<P class=docList>Makes the method specified by
<TT>symbol</TT> private. The method must have been
previously defined. If no arguments are specified, the
visibility of all subsequently defined methods in the class
or module is made private. </P></DD></DL><A
name=ruby-CHP-2-SECT-9.7></A>
<H4 class=docSection2Title>2.9.7 Object Initialization</H4><A
name=IXT-2-127113></A>
<P class=docText><A name=IXT-2-127114></A><A
name=IXT-2-127115></A><A name=IXT-2-127116></A><A
name=IXT-2-127117></A><A name=IXT-2-127118></A>Objects are
created using the <TT>new</TT> method of each object's class.
After a <TT>new</TT> object is created by the <TT>new</TT>
method, the object's <TT>initialize</TT> method is called with
the arguments of the <TT>new</TT> method passed to it. Blocks
associated with the <TT>new</TT> method are also passed
directly to <TT>initialize</TT>. For consistency, you should
initialize objects by redefining the <TT>initialize</TT>
method, rather than the <TT>new</TT> method. The visibility of
methods named <TT>initialize</TT> is automatically made
private. </P><A name=ruby-CHP-2-SECT-9.8></A>
<H4 class=docSection2Title>2.9.8 Attributes</H4><A
name=IXT-2-127119></A>
<P class=docText><A name=IXT-2-127120></A><A
name=IXT-2-127121></A>Attributes are methods that can be
referenced and assigned to externally as if they were
variables. For example, the <TT>Process</TT> module attribute
<TT>egid</TT> can be manipulated in the following way: </P><PRE>Process.egid # Reference
Process.egid=id # Assignment</PRE>
<P class=docText><A name=IXT-2-127122></A><A
name=IXT-2-127123></A><A name=IXT-2-127124></A><A
name=IXT-2-127125></A>These are actually two methods, one that
takes no argument and another with a name ending with
<TT>=</TT> that takes one argument. Methods that form such
attributes are referred to as <SPAN
class=docEmphasis>accessor</SPAN> methods. </P><A
name=ruby-CHP-2-SECT-9.9></A>
<H4 class=docSection2Title>2.9.9 Hooks</H4><A
name=IXT-2-127126></A>
<P class=docText>Ruby notifies you when a certain event
happens, as shown in <A class=docLink
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#ruby-CHP-2-TABLE-2">Table
2-2</A>. </P><A name=ruby-CHP-2-TABLE-2></A>
<P>
<TABLE cellSpacing=0 cellPadding=1 width="100%" border=1>
<CAPTION>
<H5 class=docTableTitle>Table 2-2. Events and their hook
methods </H5></CAPTION>
<COLGROUP span=3>
<TBODY>
<TR>
<TH class=docTableHeader>
<P class=docText>Event</P></TH>
<TH class=docTableHeader>
<P class=docText>Hook method</P></TH>
<TH class=docTableHeader>
<P class=docText>Of</P></TH></TR>
<TR>
<TD class=docTableCell>
<P class=docText>Defining an instance method</P></TD>
<TD class=docTableCell><PRE>method_added</PRE></TD>
<TD class=docTableCell>
<P class=docText>Class</P></TD></TR>
<TR>
<TD class=docTableCell>
<P class=docText>Defining a singleton method</P></TD>
<TD class=docTableCell><PRE>singleton_method_added</PRE></TD>
<TD class=docTableCell>
<P class=docText>Object</P></TD></TR>
<TR>
<TD class=docTableCell>
<P class=docText>Make subclass</P></TD>
<TD class=docTableCell><PRE>inherited</PRE></TD>
<TD class=docTableCell>
<P
class=docText>Superclass</P></TD></TR></COLGROUP></TBODY></TABLE></P>
<P class=docText>These methods are called
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -