没有合适的资源?快使用搜索试试~ 我知道了~
Ruby_Programming
需积分: 10 8 下载量 179 浏览量
2017-10-30
14:25:05
上传
评论
收藏 1019KB PDF 举报
温馨提示
本书为日本公认的最好的Ruby入门教程。松本行弘亲自审校并作序推荐。本书支持最新的Ruby 2.0,也附带讲解了可运行于1.9版本的代码,事无巨细且通俗易懂地讲解了编写程序时所需要的变量、常量、方法、类、流程控制等的语法,以及主要类的使用方法和简单的应用,让没有编程经验的读者也能轻松掌握Ruby,找到属于自己的快乐编程方式,做到融会贯通并灵活运用到实际工作中。 本书适合Ruby初学者学习参考。
资源推荐
资源详情
资源评论









Ruby Programming
Wikibooks.org

December 1, 2012
On the 28th of April 2012 the contents of the English as well as German Wikibooks and Wikipedia projects were
licensed under Creative Commons Attribution-ShareAlike 3.0 Unported license. An URI to this license is given
in the list of figures on page 249. If this document is a derived work from the contents of one of these projects
and the content was still licensed by the project under this license at the time of derivation this document has
to be licensed under the same, a similar or a compatible license, as stated in section 4b of the license. The
list of contributors is included in chapter Contributors on page 243. The licenses GPL, LGPL and GFDL are
included in chapter Licenses on page 253, since this book and/or parts of it may or may not be licensed under
one or more of these licenses, and thus require inclusion of these licenses. The licenses of the figures are given
in the list of figures on page 249. This PDF was generated by the L
A
T
E
X typesetting software. The L
A
T
E
X source
code is included as an attachment (
source.7z.txt
) in this PDF file. To extract the source from the PDF
file, we recommend the use of
http://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/
utility or clicking the paper clip attachment symbol on the lower left of your PDF Viewer, selecting
Save
Attachment
. After extracting it from the PDF file you have to rename it to
source.7z
. To uncompress
the resulting archive we recommend the use of
http://www.7-zip.org/
. The L
A
T
E
X source itself
was generated by a program written by Dirk HÃijnniger, which is freely available under an open source
license from
http://de.wikibooks.org/wiki/Benutzer:Dirk_Huenniger/wb2pdf
. This
distribution also contains a configured version of the
pdflatex
compiler with all necessary packages and
fonts needed to compile the L
A
T
E
X source included in this PDF file.

Contents
1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.1 Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2 Installing Ruby . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.1 Operating systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.2 Building from Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.3 Compile options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.4 Testing Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.5 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3 Ruby editors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
4 Notation conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.1 Command-line examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
5 Interactive Ruby . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
5.1 Running irb . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
5.2 Understanding irb output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
6 Mailing List FAQ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
7 Basic Ruby - Hello world . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
7.1 Hello world . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
7.2 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
7.3 Executable Ruby scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
8 Basic Ruby - Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
8.1 String literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
8.2 Single quotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
8.3 Double quotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
8.4 puts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
8.5 print . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
8.6 See also . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
9 Basic Ruby - Alternate quotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
9.1 Alternate single quotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
9.2 Alternate double quotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
10 Basic Ruby - Here documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
10.1 Here documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
10.2 Indenting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
10.3 Quoting rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
11 Basic Ruby - Introduction to objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
11.1 What is an object? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
11.2 Variables and objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
11.3 Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
11.4 Reassigning a variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
12 Basic Ruby - Ruby basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
III

Contents
13 Dealing with variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
14 Program flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
15 Writing functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
16 Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
17 Ruby is really, really object-oriented . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
18 Basic Ruby - Data types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
18.1 Ruby Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
18.2 Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
18.3 Symbols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
18.4 Hashes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
18.5 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
18.6 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
18.7 Numbers (Integers and Floats) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
18.8 Additional String Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
19 Basic Ruby - Writing methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
19.1 Defining Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
20 Basic Ruby - Classes and objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
20.1 Ruby Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
20.2 Creating Instances of a Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
20.3 Creating Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
20.4 Self . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
20.5 Class Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
21 Basic Ruby - Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
22 Syntax - Lexicology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
22.1 Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
22.2 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
22.3 Embedded Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
22.4 Reserved Words . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
22.5 Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
23 Syntax - Variables and Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
23.1 Local Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
23.2 Instance Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
23.3 Class Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
23.4 Global Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
23.5 Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
23.6 Pseudo Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
23.7 Pre-defined Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
23.8 Pre-defined Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
23.9 Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
24 Syntax - Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
24.1 Numerics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
24.2 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
24.3 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
24.4 Hashes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
24.5 Ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
25 Syntax - Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
25.1 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
25.2 Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
IV

Contents
25.3 Default scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
25.4 Local scope gotchas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
25.5 Logical And . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
25.6 Logical Or . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
26 Syntax - Control Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
26.1 Control Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
27 Syntax - Method Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
27.1 Method Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
27.2 Method Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
27.3 Dynamic methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
27.4 Special methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
27.5 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
28 Syntax - Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
28.1 Class Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
28.2 Declaring Visibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
28.3 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
28.4 Mixing in Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
28.5 Ruby Class Meta-Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
29 Syntax - Hooks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
29.1 const_missing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
30 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
31 Built-In Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
32 Predefined Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
33 Predefined Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
33.1 Footnotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
34 Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
35 Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
36 Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
37 Comparable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
38 Encoding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
39 Enumerable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
39.1 Enumerable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
40 Forms of Enumerator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
40.1 1. As a proxy for “each” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
40.2 2. As a source of values from a block . . . . . . . . . . . . . . . . . . . . . . . . . 157
40.3 3. As an external iterator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
41 Lazy evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
42 Methods which return Enumerators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
43 More Enumerator readings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
44 Exception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
45 FalseClass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
46 IO - Fiber . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
47 IO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
47.1 Encoding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
47.2 gets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
47.3 recv . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
47.4 read . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
48 IO - File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
V
剩余260页未读,继续阅读
资源评论

- #完美解决问题
- #运行顺畅
- #内容详尽
- #全网独家
- #注释完整

liani1028
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- Proteus 仿真MSP430实例之10 ADC12.zip
- Proteus 仿真MSP430实例之09 SPI.zip
- 基于两电平逆变器的SVPWM调制技术及空间矢量优化在Simulink仿真模型与Visio原理图中的实践与应用研究 ,基于两电平逆变器的SVPWM调制算法研究 - 文件包含Simulink仿真模型与Vi
- 洛谷P2089代码题解
- 基于事件触发机制的多智能体系统跟踪共识与共识策略研究,基于事件触发机制的多智能体系统跟踪共识研究:非理想一般线性系统的共识策略与算法设计,基于观测器的非理想一般线性多智能体系统的事件触发跟踪共识 关键
- MATLAB仿真:基于人工势场法的无人车路径规划算法Demo,含目标定位与障碍物避障,详细代码注释助力初学者快速掌握 ,MATLAB中基于人工势场法的无人车路径规划算法Demo:目标定位与障碍物规避的
- 三相并网仿真系统中的准PR控制策略及电容电流反馈技术应用研究,三相并网仿真技术:准PR控制与电容电流反馈策略的探讨与实践,三相并网仿真,准PR控制+电容电流反馈 ,三相并网仿真; 准PR控制; 电容电
- 基于改进麻雀算法的径向基神经网络优化与回归预测建模:Excel数据驱动的误差评估与可视化,基于改进麻雀算法的径向基神经网络优化与回归预测建模:Excel数据驱动的误差评估与可视化分析,改进麻雀算法优化
- 正齿轮减速机 - 变速箱 正齿轮减速器变速箱 3D 在 Creo 11 中设计 包括 STEP 组件
- 基于虚拟惯性控制与下垂控制的双馈风力发电机多机多节点一次调频模型:高风电渗透率下的储能与光伏系统整合仿真研究,基于虚拟惯性控制与下垂控制的双馈风力发电机多机多节点一次调频模型:仿真与接入混合能源系统研
- 基于ECMS的并联混合动力汽车能量管理策略研究:实现最小等效燃油消耗与多工况仿真分析,基于ECMS能量管理策略的并联混合动力汽车仿真研究:实现等效燃油消耗最小化与性能优化,ECMS,等效燃油消耗最小策
- 基于Matlab Simulink的双馈风机并网仿真模型:高效稳定、自调节与测频功能实现,基于Matlab Simulink的双馈风机并网模型:可测频自调波形稳定,仿真速度快捷高效,风机并网matla
- 标题:基于GWO-LSSVM灰狼算法优化最小二乘支持向量机回归预测的易用型软件工具-适应度函数优化参数与结果存储升级版 注:标题字数控制在40字以上,90字以内,根据您提供的文字提炼得出,力求简洁
- 分布式驱动电动汽车路面附着系数精确估计技术研究:基于无迹卡尔曼滤波(UKF)与容积卡尔曼滤波(CKF)的S-function估计算法及其在不同工况下的应用评估 ,基于分布式驱动电动汽车的动态路面附着系
- 基于蛇优化器(Snake Optimizer, SO)的无人机路径规划技术:探索算法在科学和工程应用中的高效性能与改进空间,**基于自然启发的蛇优化器在无人机路径规划的高效应用:提升算法质量与现实实践
- MDK5.41(含科学使用文件)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制
