📄 2.4.htm
字号:
<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-4&open=false&catid=&s=1&b=1&f=1&t=1&c=1&u=1"
target=_new><IMG height=12 src="2.4.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-4&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-4&open=false&catid=&s=1&b=1&f=1&t=1&c=1&u=1"><IMG
height=9 hspace=3 src="2.4.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-4&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-4&open=false&catid=&s=1&b=1&f=1&t=1&c=1&u=1"><IMG
height=14 hspace=3 src="2.4.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-4&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-3&open=false&catid=&s=1&b=1&f=1&t=1&c=1&u=1"><IMG
height=16 src="2.4.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-5&open=false&catid=&s=1&b=1&f=1&t=1&c=1&u=1"><IMG
height=16 src="2.4.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.4.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-4></A>
<H3 class=docSection1Title>2.4 Literals</H3><A
name=ruby-IDXTERM-135></A>
<P class=docText>I've often wondered why we programmers are so
enamored with literals. I'm waiting for the day when a
language comes along and introduces "figuratives." In the
interim, the rules Ruby uses for literals are simple and
intuitive, as you'll see the following sections. </P><A
name=ruby-CHP-2-SECT-4.1></A>
<H4 class=docSection2Title>2.4.1 Numbers</H4><A
name=IXT-2-126904></A>
<P class=docText><A name=IXT-2-126905></A><A
name=IXT-2-126906></A><A name=IXT-2-126907></A>Strings and
numbers are the bread and butter of literals. Ruby provides
support for both integers and floating-point numbers, using
classes <TT>Fixnum</TT>, <TT>Bignum</TT>, and <TT>Float</TT>.
</P><A name=ruby-CHP-2-SECT-4.1.1></A>
<H5 class=docSection3Title>2.4.1.1 Integers</H5>
<P class=docText><A name=IXT-2-126908></A><A
name=IXT-2-126909></A>Integers are instances of class
<TT>Fixnum</TT> or <TT>Bignum</TT>: </P><PRE>123 # decimal
1_234 # decimal with underline
0377 # octal
0xff # hexadecimal
0b1011 # binary
?a # character code for 'a'
12345678901234567890 # Bignum: an integer of infinite length</PRE><A
name=ruby-CHP-2-SECT-4.1.2></A>
<H5 class=docSection3Title>2.4.1.2 Floating-point numbers</H5>
<P class=docText>Floating-point numbers are instances of class
<TT>Float</TT>: </P><PRE>123.4 # floating point value
1.0e6 # scientific notation
4E20 # dot not required
4e+20 # sign before exponential</PRE><A
name=ruby-CHP-2-SECT-4.2></A>
<H4 class=docSection2Title>2.4.2 Strings</H4>
<P class=docText><A name=IXT-2-126910></A>A string is an array
of bytes (octets) and an instance of class <TT>String</TT>:
</P><A name=IXT-2-126911></A><A name=IXT-2-126912></A>
<DL class=docList>
<DT><SPAN class=docPubcolor><SPAN class=docPubcolor><SPAN
class=docMonofont>"abc"</SPAN></SPAN></SPAN>
<DD>
<P class=docList><A name=IXT-2-126911></A><A
name=IXT-2-126912></A>Double-quoted strings allow
substitution and backslash notation. </P>
<DT><SPAN class=docPubcolor><SPAN class=docPubcolor><SPAN
class=docMonofont>'abc'</SPAN></SPAN></SPAN>
<DD>
<P class=docList>Single-quoted strings don't allow
substitution and allow backslash notation only for
<TT>\\</TT> and <TT>\'</TT>. </P></DD></DL><A
name=ruby-CHP-2-SECT-4.2.1></A>
<H5 class=docSection3Title>2.4.2.1 String concatenation</H5><A
name=IXT-2-126913></A>
<P class=docText><A name=IXT-2-126914></A>Adjacent strings are
concatenated at the same time Ruby parses the program. </P><PRE>"foo" "bar" # means "foobar"</PRE><A
name=ruby-CHP-2-SECT-4.2.2></A>
<H5 class=docSection3Title>2.4.2.2 Expression
substitution</H5><A name=IXT-2-126915></A>
<P class=docText><TT>#$var</TT><A name=IXT-2-126916></A> and
<TT>#@var</TT> are abbreviated forms of #{<TT>$var</TT>} and
#{<TT>@var</TT>}. Embeds value of expression in
<TT>#{...}</TT> into a string. </P><A
name=ruby-CHP-2-SECT-4.2.3></A>
<H5 class=docSection3Title>2.4.2.3 Backslash notation</H5><A
name=IXT-2-126917></A>
<P class=docText>In double-quoted strings, regular expression
literals, and command output, backslash notation can be
represent unprintable characters, 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-4&open=false&catid=&s=1&b=1&f=1&t=1&c=1&u=1#ruby-CHP-2-TABLE-1">Table
2-1</A>. </P><A name=ruby-CHP-2-TABLE-1></A>
<P>
<TABLE cellSpacing=0 cellPadding=1 width="100%" border=1>
<CAPTION>
<H5 class=docTableTitle>Table 2-1. Backslash
notations</H5></CAPTION>
<COLGROUP span=2>
<TBODY>
<TR>
<TH class=docTableHeader>
<P class=docText>Sequence</P></TH>
<TH class=docTableHeader>
<P class=docText>Character represented</P></TH></TR>
<TR>
<TD class=docTableCell><PRE>\n</PRE></TD>
<TD class=docTableCell>
<P class=docText>Newline (0x0a)</P></TD></TR>
<TR>
<TD class=docTableCell><PRE>\r</PRE></TD>
<TD class=docTableCell>
<P class=docText>Carriage return (0x0d)</P></TD></TR>
<TR>
<TD class=docTableCell><PRE>\f</PRE></TD>
<TD class=docTableCell>
<P class=docText>Formfeed (0x0c)</P></TD></TR>
<TR>
<TD class=docTableCell><PRE>\b</PRE></TD>
<TD class=docTableCell>
<P class=docText>Backspace (0x08)</P></TD></TR>
<TR>
<TD class=docTableCell><PRE>\a</PRE></TD>
<TD class=docTableCell>
<P class=docText>Bell (0x07)</P></TD></TR>
<TR>
<TD class=docTableCell><PRE>\e</PRE></TD>
<TD class=docTableCell>
<P class=docText>Escape (0x1b)</P></TD></TR>
<TR>
<TD class=docTableCell><PRE>\s</PRE></TD>
<TD class=docTableCell>
<P class=docText>Space (0x20)</P></TD></TR>
<TR>
<TD class=docTableCell><PRE>\nnn</PRE></TD>
<TD class=docTableCell>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -