Ruby 小抄

快速入门 Ruby 语法。

Ruby 安装

实用网站

标识符

一般习惯使用下划线进行单词分隔

注释

1
2
3
4
5
6
# 单行注释

=begin
多行
注释
=end

变量/常量

1
2
3
name = 'Ruby'     # 变量

VERSION = '3.2.1' # 常量

字符串

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
lang = 'ruby'
msg = "I am using #{lang}" # I am using ruby
msg = 'I am using #{lang}' # I am using ${lang}

# 多行字符串
puts <<-S



S
# 有
# 多
# 行

puts <<~MESSAGE
自动
删除
缩进
MESSAGE
# 自动
# 删除
# 缩进

数组

1
2
3
4
5
6
7
a = ['a', 'b', 'c']
b = ['a', 1, 2, 3]

a + b # ["a", "b", "c", "a", 1, 2, 3]
a - b # ["b", "c"]
a | b # ["a", "b", "c", 1, 2, 3]
a & b # ["a"]

哈希表

1
2
3
4
5
user = {
id: 1,
name: 'ruby'
}
user[:name] # ruby

空判断

1
2
3
# 当 a 为 nil 时,赋值为 100
a ||= 100
a # 100

赋值

1
2
3
a, b = [1, 2]
a # 1
b # 2

方法调用

1
2
3
4
5
f1            # 无参省略小括号(推荐)
f1() # 无参保留小括号

puts 'hi' # 省略小括号
puts('hi') # 保留小括号

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Foo
def initialize(name)
@name = name
end

def say
puts "Hi, #{@name}!"
end

def self.bar
puts 'Foo, bar!'
end
end

foo = Foo.new 'Jack'
foo.say # Hi, Jack!

Foo.bar # Foo, bar!

继承

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Person
def initialize(name)
@name = name
end

def say
puts "Hi, I am #{@name}"
end
end

class Student < Person
def initialize(name, grade)
super(name)
@grade = grade
end

def get_grade
puts "I am grade #{@grade}"
end
end

stu = Student.new('Jack', 2)
stu.say # Hi, I am Jack
stu.get_grade # I am grade 2

访问权限

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Foo
def public_method
puts 'public method'
end

protected
def protected_method
puts 'protected method'
end

private
def private_method
puts 'private method'
end
end

foo = Foo.new
foo.public_method # public method
foo.protected_method # ERROR! protected method `protected_method' called for #<Foo:0x00000001010316f8> (NoMethodError)
foo.private_method # ERROR! private method `private_method' called for #<Foo:0x00000001032a61c0> (NoMethodError)