📄 4.1.htm
字号:
<TD class=t1 vAlign=bottom>Chapter 4. Standard Library
Reference</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-4-SECT-1></A>
<H3 class=docSection1Title>4.1 Standard Library</H3><A
name=IXT-4-128216></A>
<P class=docText>The Ruby standard library extends the
foundation of the Ruby built-in library with classes and
abstractions for a variety of programming needs, including
network programming, operating-system services, threads, and
more. These classes provide flexible capabilities at a high
level of abstraction, giving you the ability to create
powerful Ruby scripts useful in a variety of problem domains.
</P>
<P class=docText>Many common tasks are performed by Ruby
programmers all over the world. Some of these tasks include
network access such as TCP/IP and CGI, OS access, database
access, controlling processes with threads, numeric
calculations, implementing design classes, and manipulating
dates. These are used so frequently that they are included
with all standard distributions of Ruby; when you access these
classes and methods from your programs, they will be available
from the Standard Library. Could you write these libraries
yourself? Probably. Would you feel confident they have been
exhaustively tested, optimized, and debugged? Usually not. The
Standard Library is a great time saver. And as Ruby grows and
evolves, so will its Standard Library, to everyone's benefit.
</P>
<P class=docText>Although not every library section will
contain all these entries, the basic format for each section
is as follows: </P>
<UL>
<LI>
<P class=docList>Required library</P>
<LI>
<P class=docList>Example</P>
<LI>
<P class=docList>Inherited class</P>
<LI>
<P class=docList>Class methods</P>
<LI>
<P class=docList>Instance methods</P></LI></UL><A
name=ruby-CHP-4-SECT-1.1></A>
<H4 class=docSection2Title>4.1.1 Network</H4>
<P class=docText>Use Ruby's network classes to let your
scripts speak basic protocols such as TCP and UDP as a client,
a server, or both. These libraries provide socket access to a
variety of Internet protocols and classes that make access to
those protocols easier. You can even crawl up the protocol
stack and find support for higher-level protocols like FTP,
HTTP, IMAP, and so on. All have an intuitive, transparent
interface that won't get in your way. This is the largest
group of libraries and one of the most frequently used. </P>
<P class=docText>Oh, and don't worry. There's support for
doing web programming through the CGI, <TT>CGI::Cookie</TT>
and <TT>CGI::Session</TT> classes. </P><A
name=ch04-4-fm2xml></A><A name=IXT-4-128217></A><A
name=IXT-4-128218></A><A name=IXT-4-128219></A><A
name=IXT-4-128220></A><A name=IXT-4-128221></A><A
name=IXT-4-128222></A><A name=IXT-4-128223></A><A
name=IXT-4-128224></A><A name=IXT-4-128225></A><A
name=IXT-4-128226></A><A name=IXT-4-128227></A>
<TABLE cellPadding=5 width=515 border=0>
<TBODY>
<TR>
<TD align=left><B><I>BasicSocket</I></B></TD>
<TD align=right><I>Socket-related superclass
</I></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><TT>BasicSocket</TT><A name=IXT-4-128217></A>
is an abstract base class for network socket-related classes.
This class provides common behavior among <TT>Socket</TT>
classes. </P><A name=ch04-5-fm2xml></A>
<H4 class=docRefsectTitle>Required Library</H4>
<P class=docText>require 'socket'</P><A
name=ch04-6-fm2xml></A>
<H4 class=docRefsectTitle>Inherited Class</H4><A
name=IXT-4-128218></A>
<P class=docText><TT>IO</TT></P><A name=ch04-7-fm2xml></A>
<H4 class=docRefsectTitle>Class Methods</H4><A
name=IXT-4-128219></A>
<DL class=docList>
<DT><SPAN class=docPubcolor><SPAN class=docPubcolor><SPAN
class=docMonofont>BasicSocket::do_not_reverse_lookup</SPAN></SPAN><A
name=IXT-4-128219></A></SPAN>
<DD>
<P class=docList>Returns <TT>true</TT> if a query returns
numeric address, not hostname </P>
<DT><SPAN class=docPubcolor><SPAN class=docPubcolor><SPAN
class=docMonofont>BasicSocket::do_not_reverse_lookup=</SPAN></SPAN>
<TT><I>bool</I></TT></SPAN>
<DD>
<P class=docList>Sets <TT>reverse_lookup</TT>
status</P></DD></DL><A name=ch04-8-fm2xml></A>
<H4 class=docRefsectTitle>Instance Methods</H4><A
name=IXT-4-128220></A><A name=IXT-4-128221></A><A
name=IXT-4-128222></A><A name=IXT-4-128223></A><A
name=IXT-4-128224></A><A name=IXT-4-128225></A><A
name=IXT-4-128226></A><A name=IXT-4-128227></A>
<DL class=docList>
<DT><SPAN class=docPubcolor><TT><I>s</I></TT><A
name=IXT-4-128221></A><SPAN class=docPubcolor><SPAN
class=docMonofont>.getpeername</SPAN></SPAN> </SPAN>
<DD>
<P class=docList>Returns information on this connection's
peer socket as a <TT>struct</TT> <TT>sockaddr</TT> packed
into a string. </P>
<DT><SPAN class=docPubcolor><TT><I>s</I></TT><A
name=IXT-4-128222></A><SPAN class=docPubcolor><SPAN
class=docMonofont>.getsockname</SPAN></SPAN> </SPAN>
<DD>
<P class=docList>Returns information on <TT><I>s</I></TT> as
a <TT>struct sockaddr</TT> packed into a string. </P>
<DT><SPAN class=docPubcolor><TT><I>s</I></TT><A
name=IXT-4-128223></A><SPAN class=docPubcolor><SPAN
class=docMonofont>.getsockopt(</SPAN></SPAN>
<TT><I>lev</I></TT><SPAN class=docPubcolor><SPAN
class=docMonofont>,</SPAN></SPAN>
<TT><I>optname</I></TT><SPAN class=docPubcolor><SPAN
class=docMonofont>)</SPAN></SPAN> </SPAN>
<DD>
<P class=docList>Gets the specified socket option. </P>
<DT><SPAN class=docPubcolor><TT><I>s</I></TT><A
name=IXT-4-128224></A><SPAN class=docPubcolor><SPAN
class=docMonofont>.setsockopt(</SPAN></SPAN>
<TT><I>lev</I></TT><SPAN class=docPubcolor><SPAN
class=docMonofont>,</SPAN></SPAN>
<TT><I>optname</I></TT><SPAN class=docPubcolor><SPAN
class=docMonofont>,</SPAN></SPAN> <TT><I>value</I></TT><SPAN
class=docPubcolor><SPAN class=docMonofont>)</SPAN></SPAN>
</SPAN>
<DD>
<P class=docList>Sets the specified socket option. </P>
<DT><SPAN class=docPubcolor><TT><I>s</I></TT><A
name=IXT-4-128225></A><SPAN class=docPubcolor><SPAN
class=docMonofont>.shutdown([</SPAN></SPAN>
<TT><I>how</I></TT><SPAN class=docPubcolor><SPAN
class=docMonofont>=2])</SPAN></SPAN> </SPAN>
<DD>
<P class=docList>Shuts down the socket connection.
<TT>0</TT> shuts down receiving, <TT>1</TT> sending, and
<TT>2</TT> both. </P>
<DT><SPAN class=docPubcolor><TT><I>s</I></TT><A
name=IXT-4-128226></A><SPAN class=docPubcolor><SPAN
class=docMonofont>.recv(</SPAN></SPAN>
<TT><I>len</I></TT><SPAN class=docPubcolor><SPAN
class=docMonofont>[,</SPAN></SPAN>
<TT><I>flags</I></TT><SPAN class=docPubcolor><SPAN
class=docMonofont>])</SPAN></SPAN> </SPAN>
<DD>
<P class=docList>Receives data from <TT><I>s</I></TT>, and
returns it as a string. </P>
<DT><SPAN class=docPubcolor><TT><I>s</I></TT><A
name=IXT-4-128227></A><SPAN class=docPubcolor><SPAN
class=docMonofont>.send(</SPAN></SPAN>
<TT><I>mesg</I></TT><SPAN class=docPubcolor><SPAN
class=docMonofont>,</SPAN></SPAN> <TT><I>flags</I></TT><SPAN
class=docPubcolor><SPAN class=docMonofont>[,</SPAN></SPAN>
<TT><I>to</I></TT><SPAN class=docPubcolor><SPAN
class=docMonofont>])</SPAN></SPAN> </SPAN>
<DD>
<P class=docList>Sends data over the socket
<TT><I>s</I></TT>, returning the length of the data sent.
<TT><I>to</I></TT> may be a <TT>struct sockaddr</TT> packed
into a string indicating the recipient address.
</P></DD></DL><A name=ch04-9-fm2xml></A><A
name=IXT-4-128228></A><A name=ruby-IDXTERM-1511></A><A
name=IXT-4-128229></A><A name=IXT-4-128230></A><A
name=IXT-4-128231></A><A name=IXT-4-128232></A><A
name=IXT-4-128233></A><A name=IXT-4-128234></A>
<TABLE cellPadding=5 width=515 border=0>
<TBODY>
<TR>
<TD align=left><B><I>IPSocket</I></B></TD>
<TD align=right><I>IP socket class
</I></TD></TR></TBODY></TABLE>
<HR align=left width=515 color=black noShade SIZE=3>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -