summaryrefslogtreecommitdiffhomepage
path: root/zh-cn/ruby-cn.html.markdown
blob: 619e6e923e2cf5485f47b2997e00bea913628c33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
---
language: ruby
filename: learnruby-zh.rb
lang: zh-cn
contributors:
  - ["David Underwood", "http://theflyingdeveloper.com"]
  - ["Joel Walden", "http://joelwalden.net"]
  - ["Luke Holder", "http://twitter.com/lukeholder"]
translators:
  - ["Lin Xiangyu", "https://github.com/oa414"]
---

```ruby
# 这是单行注释

=begin
这是多行注释
没人用这个
你也不该用
=end

# 首先,也是最重要的,所有东西都是对象

# 数字是对象

3.class #=> Fixnum

3.to_s #=> "3"


# 一些基本的算术符号
1 + 1 #=> 2
8 - 1 #=> 7
10 * 2 #=> 20
35 / 5 #=> 7

# 算术符号只是语法糖而已
# 实际上是调用对象的方法
1.+(3) #=> 4
10.* 5 #=> 50 

# 特殊的只也是对象
nil # 空
true # 真
false # 假

nil.class #=> NilClass
true.class #=> TrueClass
false.class #=> FalseClass

# 相等运算符
1 == 1 #=> true
2 == 1 #=> false

# 不等运算符
1 != 1 #=> false
2 != 1 #=> true
!true  #=> false
!false #=> true

# 除了false自己,nil是唯一的值为false的对象 

!nil   #=> true
!false #=> true
!0     #=> false

# 更多比较
1 < 10 #=> true
1 > 10 #=> false
2 <= 2 #=> true
2 >= 2 #=> true

# 字符串是对象

'I am a string'.class #=> String
"I am a string too".class #=> String

placeholder = "use string interpolation"
"I can #{placeholder} when using double quoted strings"
#=> "I can use string interpolation when using double quoted strings"


# 输出值
puts "I'm printing!"

# 变量
x = 25 #=> 25
x #=> 25

# 注意赋值语句返回了赋的值
# 这意味着你可以用多重赋值语句

x = y = 10 #=> 10
x #=> 10
y #=> 10

# 按照惯例,用 snake_case 作为变量名
snake_case = true

# 使用具有描述性的运算符
path_to_project_root = '/good/name/'
path = '/bad/name/'

# 符号(Symbols,也是对象)
# 符号是不可变的,内部用整数类型表示的可重用的值。
# 通常用它代替字符串来有效地表示有意义的值。


:pending.class #=> Symbol

status = :pending

status == :pending #=> true

status == 'pending' #=> false

status == :approved #=> false

# 数组

# 这是一个数组
[1, 2, 3, 4, 5] #=> [1, 2, 3, 4, 5]

# 数组可以包含不同类型的元素

array = [1, "hello", false] #=> => [1, "hello", false]

# 数组可以被索引
# 从前面开始
array[0] #=> 1
array[12] #=> nil

# 像运算符一样,[var]形式的访问
# 也就是一个语法糖
# 实际上是调用对象的[] 方法
array.[] 0 #=> 1
array.[] 12 #=> nil

# 从尾部开始
array[-1] #=> 5

# 同时指定开始的位置和结束的位置
array[2, 4] #=> [3, 4, 5]

# 或者指定一个范围
array[1..3] #=> [2, 3, 4]

# 像这样往数组增加一个元素
array << 6 #=> [1, 2, 3, 4, 5, 6]

# 哈希表是Ruby的键值对的基本数据结构
# 哈希表由大括号定义
hash = {'color' => 'green', 'number' => 5}

hash.keys #=> ['color', 'number']

# 哈希表可以通过键快速地查询
hash['color'] #=> 'green'
hash['number'] #=> 5

# 查询一个不存在地键将会返回nil
hash['nothing here'] #=> nil

# 用 #each 方法来枚举哈希表:
hash.each do |k, v|
  puts "#{k} is #{v}"
end

# 从Ruby 1.9开始, 用符号作为键的时候有特别的记号表示:

new_hash = { defcon: 3, action: true}

new_hash.keys #=> [:defcon, :action]

# 小贴士:数组和哈希表都是可枚举的
# 它们可以共享一些有用的方法,比如each, map, count, 和more

# 控制流

if true
  "if statement"
elsif false
 "else if, optional"
else
 "else, also optional"
end

for counter in 1..5
  puts "iteration #{counter}"
end
#=> iteration 1
#=> iteration 2
#=> iteration 3
#=> iteration 4
#=> iteration 5

# 然而
# 没人用for循环
#  用`each`来代替,就像这样

(1..5).each do |counter|
  puts "iteration #{counter}"
end
#=> iteration 1
#=> iteration 2
#=> iteration 3
#=> iteration 4
#=> iteration 5

counter = 1
while counter <= 5 do
  puts "iteration #{counter}"
  counter += 1
end
#=> iteration 1
#=> iteration 2
#=> iteration 3
#=> iteration 4
#=> iteration 5

grade = 'B'

case grade
when 'A'
  puts "Way to go kiddo"
when 'B'
  puts "Better luck next time"
when 'C'
  puts "You can do better"
when 'D'
  puts "Scraping through"
when 'F'
  puts "You failed!"
else 
  puts "Alternative grading system, eh?"
end

# 函数

def double(x)
  x * 2
end

# 函数 (以及所有的方法块) 隐式地返回了最后语句的值
double(2) #=> 4

# 当不存在歧义的时候括号是可有可无的
double 3 #=> 6

double double 3 #=> 12

def sum(x,y)
  x + y
end

# 方法的参数通过逗号分隔
sum 3, 4 #=> 7

sum sum(3,4), 5 #=> 12

# yield
# 所有的方法都有一个隐式的块参数
# 可以用yield参数调用

def surround
  puts "{"
  yield
  puts "}"
end

surround { puts 'hello world' }

# {
# hello world
# }


# 用class关键字定义一个类
class Human

     # 一个类变量,它被这个类地所有实例变量共享
    @@species = "H. sapiens"

    # 构造函数
    def initialize(name, age=0)
        # 将参数name的值赋给实例变量@name
        @name = name
        # 如果没有给出age, 那么会采用参数列表中地默认地值
        @age = age
    end

    # 基本的 setter 方法
    def name=(name)
        @name = name
    end

    # 基本地 getter 方法
    def name
        @name
    end

    # 一个类方法以self.开头
    # 它可以被类调用,但不能被类的实例调用
    def self.say(msg)
       puts "#{msg}"
    end

    def species
        @@species
    end

end


# 类的例子
jim = Human.new("Jim Halpert")

dwight = Human.new("Dwight K. Schrute")

# 让我们来调用一些方法
jim.species #=> "H. sapiens"
jim.name #=> "Jim Halpert"
jim.name = "Jim Halpert II" #=> "Jim Halpert II"
jim.name #=> "Jim Halpert II"
dwight.species #=> "H. sapiens"
dwight.name #=> "Dwight K. Schrute"

# 调用对象的方法
Human.say("Hi") #=> "Hi"

```