Skip to content

Commit e641622

Browse files
committed
WHAT: typo
WHY: * XXXXX HOW: * NOTHING TO SAY
1 parent b580df7 commit e641622

File tree

2 files changed

+63
-25
lines changed

2 files changed

+63
-25
lines changed

Notes/Ruby/Ruby瞎探索.md

Lines changed: 62 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939

4040
## Ruby 瞎探索
4141

42+
- [Ruby style guide (zh)](https://github.com/JuanitoFatas/ruby-style-guide/blob/master/README-zhCN.md)
43+
4244
### 基本语法
4345

4446
```ruby
@@ -309,7 +311,7 @@ end
309311

310312
在方法定义时,可以给参数指定默认值,类似 ES6,并且有位置参数和关键字参数,类似 Python
311313

312-
在方法内,如果没有手动 return,则最后一个表达式的执行结果就会成为方法发返回值
314+
在方法内,如果没有手动 return,则最后一个表达式的执行结果就会成为方法返回值
313315

314316
```ruby
315317
# 默认参数
@@ -377,14 +379,15 @@ String.is_a?(Object) # true
377379

378380
```ruby
379381
class Foo
380-
# 存取器,定义 name 实例变量时可读写的
382+
# 存取器,定义 name 实例变量是可读写的
381383
attr_accessor :name
382384

383385
# ================ 初始化方法 ================
384386
def initialize(x:, y: 2, z: 3, name: 'foo')
385387
@x = x # 初始实例变量
386388
@y = y # 只有获取没有赋值,被定义为只读
387389
@z = z # 实例变量在没有存取器的时候,不能获取或者赋值
390+
@name = name
388391
end
389392

390393
def echo
@@ -418,20 +421,28 @@ end
418421

419422
foo = Foo.new(x: 1)
420423
foo.echo # x: 1, y: 2, z: 3
424+
puts foo.name # 已经定义 name 为可读写
425+
foo.name = 123
426+
puts foo.name # 123
427+
# 而 y 为只读
428+
puts foo.y # 2
429+
foo.y = 3 # error!
421430
```
422431

423432
#### 类方法/单例类方法
424433

425434
```ruby
426435
# 类方法
427-
428436
# 为类定义类方法有如下一些形式
437+
438+
# Template 1
429439
class << Foo
430440
def hello(name)
431441
puts "hello #{name}"
432442
end
433443
end
434444

445+
# Template 2
435446
class Foo
436447
class << self
437448
def hello(name)
@@ -440,12 +451,14 @@ class Foo
440451
end
441452
end
442453

454+
# Template 3
443455
class Foo
444456
def self.hello(name)
445457
puts "hello #{name}"
446458
end
447459
end
448460

461+
# Template 4
449462
def Foo.hello(name)
450463
puts "hello #{name}"
451464
end
@@ -457,6 +470,7 @@ Foo.hello('123') # 调用类方法
457470
# 单例类方法
458471
# 单例类方法即只给某个类的实例定义的方法,仅有该实例可以调用
459472

473+
# Template
460474
class << instance
461475
def func
462476
# do
@@ -485,21 +499,21 @@ str2.hello # error
485499

486500
=begin
487501
实例变量:以 @ 开头,提供外部访问、修改时需要定义存取器
488-
@name
502+
例如:@name
489503
490504
类变量:以 @@ 开头,也需要定义存取器,但是不能使用 attr_accessor 等便捷方式去定义
491-
@@name
505+
例如:@@name
492506
493507
常量:以首字母大写开头
494-
Name
508+
例如:Name
495509
=end
496510

497511
# 类常量通过 类名::Name 访问
498512
class Foo
499513
Version = 1
500514
end
501515

502-
puts Foo::Version
516+
puts Foo::Version # 1
503517

504518
# 类变量是所有实例的共享变量
505519
class Foo
@@ -606,6 +620,8 @@ end
606620

607621
boo = Boo.new
608622
boo.echo
623+
# boo
624+
# foo
609625
```
610626

611627
在没有指定父类时,Ruby 会默认继承`Object`类作为父类。也可以手动指定父类为`BasicObject`
@@ -657,15 +673,20 @@ end
657673
module ExampleModule
658674
Version = 1 # 定义常量
659675

660-
# 如果想能够通过 模块.方法 的形式调用模块内的方法,
661-
# 则需要通过 module_function 进行暴露
662-
module_function :echo
663676
def echo
664677
puts "example"
665678
end
679+
680+
def test
681+
puts "test"
682+
end
683+
684+
# 如果想能够通过 模块.方法 的形式调用模块内的方法,
685+
# 则需要通过 module_function 进行暴露
686+
module_function :echo
666687
end
667688

668-
ExampleModule.Version # 1
689+
puts ExampleModule::Version # 1
669690
ExampleModule.echo # "example"
670691

671692
# 类扩展模块
@@ -700,7 +721,7 @@ ExampleClass.superclass # Object
700721
=end
701722
str = "1"
702723
str.extend(ExampleModule)
703-
str.echo # "example"
724+
str.test # "test"
704725

705726
=begin
706727
而类的 extend Module 则是给类扩展类方法
@@ -791,21 +812,22 @@ end
791812

792813
def function
793814
if block_given? then
794-
echo "block_given"
815+
puts "block_given"
795816
yield
796817
else
797-
echo "no block given"
818+
puts "no block given"
798819
end
799820

800821
# Proc 参数一定要在最后一位
801822
def function(val, &block)
802823
if block then
803-
echo "has block"
824+
puts "has block"
804825
# 如果没有传入块,则 block 为 nil
805826
# 在调用 Proc 对象的 call 方法之前,块中定义的程序不会被执行
806827
block.call(val)
807828
else
808-
echo "no block"
829+
puts "no block"
830+
end
809831
end
810832

811833
# ===============================================================
@@ -884,15 +906,15 @@ num.to_c
884906
在小数和整数的末尾添加 r 即可得到 Rational 对象
885907
在小数和整数的末尾添加 i 即可得到 Complex 对象
886908
887-
在 ** 乘方运算时,指数如果为负整数,则返回有理数的 Rational 对象
909+
在 ** 乘方运算时,指数如果为负整数,则返回有理数的 Rational 对象
888910
5 ** -2 => (1/25)
889911
890912
============================================================
891913
892914
round 可以对小数进行四舍五入,并指定位数
893915
- 当参数为正数时,指定取几位小数
894916
- 当参数为负时,则往整数部分取整
895-
0.12.round(1) => 0.1
917+
0.12.round(1) => 0.1
896918
120.round(-2) => 100
897919
180.rount(-2) => 200
898920
@@ -920,7 +942,7 @@ floor 向小数的方向取整
920942

921943
# x.divmod(y) 返回商和余数的数组,相当于 [x.div(y), x.modulo(y)]
922944

923-
# x.remainder(y) 返回 x 除以 y 的余数。和 x % y 相比,该方法返回数值的符号和 x 一致
945+
# x.remainder(y) 返回 x 除以 y 的余数。和 x % y 相比,该方法返回数值的符号和 x 一致
924946
-5 % 2 # 1
925947
-5.remainder(2) # -1
926948
```
@@ -939,13 +961,25 @@ r.rand
939961
#### 方法
940962

941963
```ruby
942-
n.times { |i| ... }
964+
# n.times { |i| ... }
965+
2.times { |index|
966+
puts index
967+
}
943968

944-
from.upto(to) { |i| ... } # 相当于 from...to
969+
# from.upto(to) { |i| ... } 相当于 from...to
970+
1.upto(3) { |index|
971+
puts index
972+
}
945973

946-
from.downto(to) { |i| ... }
974+
# from.downto(to) { |i| ... }
975+
3.downto(1) { |index|
976+
puts index
977+
}
947978

948-
from.step(to, step) { |i| ... }
979+
# from.step(to, step) { |i| ... }
980+
1.step(3, 2) { |index|
981+
puts index
982+
}
949983
```
950984

951985
### 数组
@@ -984,7 +1018,7 @@ dict.to_a # [[:a, 1]]
9841018
# 索引
9851019
a[n] # 索引为负时从末尾开始获取元素
9861020
a[n..m] # 获取索引在 [n, m] 范围内的元素
987-
a[n...m] # 获取索引在 [n, m)范围内的元素
1021+
a[n...m] # 获取索引在 [n, m) 范围内的元素
9881022
a[n, len] # 获取索引在 [n, n + len) 范围内的元素
9891023

9901024
a.at(n) # 相当于 a[n]
@@ -1117,6 +1151,9 @@ a.collect! { |item| ... }
11171151
a.map { |item| ... }
11181152
a.map! { |item| ... }
11191153

1154+
arr = [1, 2, 3].collect { |item| item * 2 }
1155+
print arr # [2,4,6]
1156+
11201157
# each
11211158
a.each { |item| ... }
11221159
a.each do |item|
@@ -1287,7 +1324,7 @@ h.reject! { |key, val| ... } # 返回 nil
12871324
# 清空散列的所有键值对
12881325
h.clear
12891326

1290-
# 合并散列
1327+
# 合并散列,merge 和 update 用法相同
12911328
h1.merge(h2)
12921329
h1.merge!(h2)
12931330

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
- [Elm](./Notes/Elm)
5858
- [Ember](./Notes/ember)
5959
- [Swift4 & iOS](./Notes/iOS)
60+
- [Ruby](./Notes/Ruby)
6061
- [Haskell](./Notes/Haskell)
6162

6263
## 专题

0 commit comments

Comments
 (0)