[ruby/zh-cn] sync with english version

- sync code, result andcontributors
This commit is contained in:
woclass 2019-06-01 15:27:37 +08:00
parent cd7816a2be
commit b702586ff4

View File

@ -6,11 +6,25 @@ contributors:
- ["David Underwood", "http://theflyingdeveloper.com"]
- ["Joel Walden", "http://joelwalden.net"]
- ["Luke Holder", "http://twitter.com/lukeholder"]
- ["Tristan Hume", "http://thume.ca/"]
- ["Nick LaMuro", "https://github.com/NickLaMuro"]
- ["Marcos Brizeno", "http://www.about.me/marcosbrizeno"]
- ["Ariel Krakowski", "http://www.learneroo.com"]
- ["Dzianis Dashkevich", "https://github.com/dskecse"]
- ["Levi Bostian", "https://github.com/levibostian"]
- ["Rahil Momin", "https://github.com/iamrahil"]
- ["Gabriel Halley", "https://github.com/ghalley"]
- ["Persa Zula", "http://persazula.com"]
- ["Jake Faris", "https://github.com/farisj"]
- ["Corey Ward", "https://github.com/coreyward"]
- ["Jannik Siebert", "https://github.com/janniks"]
- ["Keith Miyake", "https://github.com/kaymmm"]
- ["lidashuang", "https://github.com/lidashuang"]
- ["ftwbzhao", "https://github.com/ftwbzhao"]
translators:
- ["Lin Xiangyu", "https://github.com/oa414"]
- ["Jiang Haiyun", "https://github.com/haiiiiiyun"]
- ["woclass", "https://github.com/inkydragon"]
---
```ruby
@ -18,20 +32,21 @@ translators:
=begin
=end
# 首先,也是最重要的,所有东西都是对象
# 在 Ruby 中,(几乎)所有东西都是对象
# 数字是对象
3.class #=> Fixnum
3.class #=> Integer
3.to_s #=> "3"
# 字符串是对象
"Hello".class #=> String
# 一些基本的算术符号
# 甚至方法也是对象
"Hello".method(:class).class #=> Method
# 一些基本的算术操作
1 + 1 #=> 2
8 - 1 #=> 7
10 * 2 #=> 20
@ -48,6 +63,7 @@ translators:
# 实际上是调用对象的方法
1.+(3) #=> 4
10.* 5 #=> 50
100.methods.include?(:/) #=> true
# 特殊的值也是对象
nil # 相当于其它语言中的 null
@ -67,10 +83,10 @@ false.class #=> FalseClass
2 != 1 #=> true
# 除了 false 自己nil 是唯一的另一个值为 false 的对象
!nil #=> true
!false #=> true
!0 #=> false
!!nil #=> false
!!false #=> false
!!0 #=> true
!!"" #=> true
# 更多比较
1 < 10 #=> true
@ -114,6 +130,7 @@ placeholder = "use string interpolation"
'hello ' + 'world' #=> "hello world"
'hello ' + 3 #=> TypeError: can't convert Fixnum into String
'hello ' + 3.to_s #=> "hello 3"
"hello #{3}" #=> "hello 3"
# 合并字符串及其运算符
'hello ' * 3 #=> "hello hello hello "
@ -174,6 +191,7 @@ array = [1, 2, 3, 4, 5] #=> [1, 2, 3, 4, 5]
# 数组可以被索引
# 从前面开始
array[0] #=> 1
array.first #=> 1
array[12] #=> nil
# 像运算符一样,[var] 形式的访问
@ -189,13 +207,13 @@ array.last #=> 5
# 同时指定开始的位置和长度
array[2, 3] #=> [3, 4, 5]
# 或者指定一个区间
array[1..3] #=> [2, 3, 4]
# 将数组逆序
a=[1,2,3]
a.reverse! #=> [3,2,1]
# 或者指定一个区间
array[1..3] #=> [2, 3, 4]
# 像这样往数组增加一个元素
array << 6 #=> [1, 2, 3, 4, 5, 6]
# 或者像这样
@ -223,6 +241,10 @@ new_hash = { defcon: 3, action: true }
new_hash.keys #=> [:defcon, :action]
# 检查键值是否存在
hash.key?(:defcon) #=> true
hash.value?(3) #=> true
# 小贴士:数组和哈希表都是可枚举的
# 它们共享一些有用的方法,比如 each, map, count 等等
@ -236,6 +258,8 @@ else
"else, also optional"
end
# 循环
for counter in 1..5
puts "iteration #{counter}"
end
@ -388,19 +412,26 @@ surround { puts 'hello world' }
# {
# hello world
# }
# => nil
# 可以向函数传递一个块
# "&"标记传递的块是一个引用
def guests(&block)
block.call 'some_argument'
block.class #=> Proc
block.call(4)
end
guests { |n| "You have #{n} guests." }
# => "You have 4 guests."
# 可以传递多个参数,这些参数会转成一个数组,
# 这也是使用星号符 ("*") 的原因:
def guests(*array)
array.each { |guest| puts guest }
end
# 结构
# 如果函数返回一个数组,在赋值时可以进行拆分:
def foods
['pancake', 'sandwich', 'quesadilla']
@ -409,19 +440,40 @@ breakfast, lunch, dinner = foods
breakfast #=> 'pancake'
dinner #=> 'quesadilla'
# 有些情况下,你会想使用解构操作符 `*` 来解构数组
ranked_competitors = ["John", "Sally", "Dingus", "Moe", "Marcy"]
def best(first, second, third)
puts "Winners are #{first}, #{second}, and #{third}."
end
best *ranked_competitors.first(3) #=> Winners are John, Sally, and Dingus.
# 结构操作符也可放在参数里面
def best(first, second, third, *others)
puts "Winners are #{first}, #{second}, and #{third}."
puts "There were #{others.count} other participants."
end
best *ranked_competitors
#=> Winners are John, Sally, and Dingus.
#=> There were 2 other participants.
# 按照惯例,所有返回布尔值的方法都以 ? 结尾
5.even? # false
5.odd? # true
# 如果方法名末尾有!,表示会做一些破坏性的操作,比如修改调用者自身。
# 很多方法都会有一个!的版本来进行修改,和一个非!的版本
# 只用来返回更新了的结果
# 如果方法名末尾有感叹号 !,表示会做一些破坏性的操作,比如修改调用者自身。
# 很多方法都会有一个 ! 的版本来进行修改,
# 和一个只返回更新结果的非 ! 版本
company_name = "Dunder Mifflin"
company_name.upcase #=> "DUNDER MIFFLIN"
company_name #=> "Dunder Mifflin"
company_name.upcase! # we're mutating company_name this time!
# 这次我们修改了 company_name
company_name.upcase! #=> "DUNDER MIFFLIN"
company_name #=> "DUNDER MIFFLIN"
# 类
# 用 class 关键字定义一个类
class Human
@ -468,7 +520,6 @@ end
# 初始化一个类
jim = Human.new("Jim Halpert")
dwight = Human.new("Dwight K. Schrute")
# 让我们来调用一些方法
@ -568,7 +619,6 @@ Book.foo # => 'foo'
Book.new.foo # => NoMethodError: undefined method `foo'
# 当包含或扩展一个模块时,相应的回调代码会被执行。
module ConcernExample
def self.included(base)
base.extend(ClassMethods)