📄 1.1.htm
字号:
end</PRE>
<DT><SPAN class=docPubcolor><SPAN
class=docEmphasis>Iterators</SPAN><A
name=IXT-1-126780></A><A name=IXT-1-126781></A><A
name=IXT-1-126782></A></SPAN>
<DD>
<P class=docList>The iterator feature for loop abstraction
is built into the language, which means a block of code can
be attached to a method call. The method can call back the
block from within its execution. For example, <TT>Array</TT>
has the <TT>each</TT> method to iterate over its contents.
With this feature, you don't need to worry about the loop
counter or boundary condition. </P><PRE>ary = [1,2,3,4,5]
ary.each do |i|
puts 1*2
end # prints 2,3,4,8,10 for each line</PRE></DD></DL>
<P class=docText><A name=IXT-1-126783></A>A block is used not
only for loops. It can be used for various purposes including
the <TT>select</TT> method of <TT>Array</TT>, which uses
blocks to choose values that satisfy conditions from contents:
</P><PRE>ary = [1,2,3,4,5]
ary = ary.select do |i|
i %2 == 0
end # returns array of even numbers.</PRE><A
name=IXT-1-126784></A><A name=IXT-1-126785></A><A
name=IXT-1-126786></A><A name=IXT-1-126787></A><A
name=IXT-1-126788></A><A name=IXT-1-126789></A><A
name=IXT-1-126790></A><A name=IXT-1-126791></A>
<DL class=docList>
<DT><SPAN class=docPubcolor><SPAN
class=docEmphasis>Exceptions</SPAN><A
name=IXT-1-126784></A></SPAN>
<DD>
<P class=docList>Just as you'd expect in a modern OOP
language, Ruby provides language-level support for exception
handling. For example, an attempt to open a file that
doesn't exist raises an exception, so that your program
doesn't run, assuming an unmet precondition. This feature
obviously enhances the reliability of your programs.
Exceptions can be caught explicitly using the
<TT>rescue</TT> clause of the <TT>begin</TT> statement: </P><PRE>begin
f = open(path)
rescue
puts "#{path} does not exist."
exit 1
end</PRE>
<DT><SPAN class=docPubcolor><SPAN class=docEmphasis>Class
libraries</SPAN><A name=IXT-1-126785></A></SPAN>
<DD>
<P class=docList>Ruby comes with a strong set of bundled
class libraries that cover a variety of domains, from basic
datatypes (strings, arrays, and hashes) to networking and
thread programming. The following program retrieves the
current time string from the local host via a network socket
connection: </P><PRE>require "socket"
print TCPSocket.open("localhost","daytime").gets</PRE>
<P class=docList><A name=IXT-1-126786></A><A
name=IXT-1-126787></A>In addition to bundled libraries, if
you go to <A class=docLink
href="http://www.ruby-lang.org/en/raa.html"
target=_blank>http://www.ruby-lang.org/en/raa.html</A> shows
a list of the many unbundled useful libraries along with
applications and documentation. Since Ruby is rather young,
the number of libraries available is smaller than that of
Perl, for example, but new libraries are becoming available
each day. </P>
<DT><SPAN class=docPubcolor><SPAN
class=docEmphasis>Portable</SPAN><A
name=IXT-1-126788></A></SPAN>
<DD>
<P class=docList>Ruby ports to many platforms, including
Unix, DOS, Windows, OS/2, etc. Ruby programs run on many
platforms without modification. </P>
<DT><SPAN class=docPubcolor><SPAN class=docEmphasis>Garbage
collection</SPAN><A name=IXT-1-126789></A></SPAN>
<DD>
<P class=docList>Object-oriented programming tends to
allocate many objects during execution. Ruby's garbage
collector recycles unused object automatically. </P>
<DT><SPAN class=docPubcolor><SPAN class=docEmphasis>Built-in
security check</SPAN><A name=IXT-1-126790></A><A
name=IXT-1-126791></A></SPAN>
<DD>
<P class=docList>Ruby's taint model provides safety when
handling untrusted data or programs. </P></DD></DL>
<UL></UL></TD></TR></TBODY></TABLE>
<HR SIZE=1>
<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-1-SECT-1&open=false&catid=&s=1&b=1&f=1&t=1&c=1&u=1"
target=_new><IMG height=12 src="1.1.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-1-SECT-1&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-1-SECT-1&open=false&catid=&s=1&b=1&f=1&t=1&c=1&u=1"><IMG
height=9 hspace=3 src="1.1.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-1-SECT-1&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-1-SECT-1&open=false&catid=&s=1&b=1&f=1&t=1&c=1&u=1"><IMG
height=14 hspace=3 src="1.1.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-1-SECT-1&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-1&open=false&catid=&s=1&b=1&f=1&t=1&c=1&u=1"><IMG
height=16 src="1.1.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-1-SECT-2&open=false&catid=&s=1&b=1&f=1&t=1&c=1&u=1"><IMG
height=16 src="1.1.files/btn_next.gif" width=41
align=absMiddle border=0></A></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD vAlign=top align=right><A class=v1
href="http://proquest.safaribooksonline.com/JVXSL.asp?x=1&mode=section&sortKey=title&sortOrder=asc&view=&xmlid=0-59600-214-9/ruby-CHP-1-SECT-1&open=false&catid=&s=1&b=1&f=1&t=1&c=1&u=1#toppage">Top</A></TD></TR></TBODY></TABLE>
<P>[<A class=v2
href="http://proquest.safaribooksonline.com/0596002149/ruby-CHP-1-SECT-1">http://proquest.safaribooksonline.com/0596002149/ruby-CHP-1-SECT-1</A>]
</P></TD></TR></TBODY></TABLE></TD>
<TD align=middle> </TD></TR>
<TR>
<TD vAlign=bottom align=middle colSpan=3><BR>
<TABLE>
<TBODY>
<TR>
<TD class=v2 vAlign=center align=middle colSpan=4
height=25> <A class=v1
title="Information About Safari Tech Books Online"
href="http://proquest.safaribooksonline.com/JVXSL.asp?mode=About">About
Safari</A> | <A class=v1
title="Safari Tech Books Online terms of service"
href="http://proquest.safaribooksonline.com/JVXSL.asp?mode=Terms">Terms
of Service</A> | <A class=v1
title="Safari Tech Books Online privacy policy"
href="http://proquest.safaribooksonline.com/JVXSL.asp?mode=Privacy">Privacy
Policy</A> | <A class=v1
title="Contacting Safari Tech Books Online"
href="http://proquest.safaribooksonline.com/JVXSL.asp?mode=Contact">Contact
Us</A> | <A class=v1
title="Safari Tech Books Online help"
href="http://proquest.safaribooksonline.com/JVXSL.asp?mode=Help">Help</A>
| <A class=v1
title="Submit a defect or enhancement request"
href="http://proquest.safaribooksonline.com/JVXSL.asp?mode=Submit">Submit
a Problem</A></TD></TR>
<TR>
<TD class=v2 vAlign=center align=middle height=20>Copyright © 2002
Safari Tech Books Online. All rights reserved.</TD></TR>
<TR vAlign=center>
<TD class=v2 vAlign=center align=middle colSpan=4 height=20>75
Arlington Street, Floor 3<BR>Boston, MA 02116<BR>800-775-7330</TD></TR>
<TR vAlign=center>
<TD class=v2 vAlign=center align=middle colSpan=4
height=5> </TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><!--
(guidin)EE94E575-D563-4A31-90-AF-CA-B8-45-75-5B-41
(guidout)EE94E575-D563-4A31-90-AF-CA-B8-45-75-5B-41
(ip)147.8.179.17
(host)proquest.safaribooksonline.com
(portal)proquest
(ref)
(base)?x=1&mode=section&sortKey=title&sortOrder=asc&view=&xmlid=0-59600-214-9/ruby-CHP-1-SECT-1&open=false&catid=&s=1&b=1&f=1&t=1&c=1&u=1
(mode)section
(xmlid)0-59600-214-9/ruby-CHP-1-SECT-1
(key)7E6FA7863F875E5D371807B896A9188D02D5BE6122A762B263800A09E833AC01645EC3A55C82E53D
(user)29536/82393
(xsl)0ms
(lib)469ms
(xml)203ms
(xsl)250ms
(ent)0ms
(buf)0ms
(tot)1219ms
--></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -