0%

异常处理

知识点

  • begin
  • rescue/else/ensure
  • end

实战演习

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
begin
#有可能发生错误的处理
puts ">处理开始"
#raise "my raise error!"
#10 / 0
rescue => e
#错误发生时
puts "X错误发生!"
puts e
else
#正常处理时
puts "O正常处理"
ensure
#最后处理,无论是否发生处理(final)
puts "_最后的扫尾处理"
end

特殊循环2

知识点

  • upto
  • downto

实战演习

1
2
3
4
5
6
7
8
9
#upto
2.upto(5) do |i|
puts "updo=" + i.to_s
end

#downto
5.downto(2) do |i|
puts "downto=" + i.to_s
end

特殊循环

知识点

  • each
  • times
  • step

实战演习

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#each循环
gamelist = ["塞尔达传说", "超级马里奥", "开心剪纸"]

gamelist.each { |game|
puts game
}

gamelist.each do |game|
puts game
end

gamelist.each_with_index do |game,i|
puts i.to_s + "." + game
end

#times循环
5.times do |i|
puts "第 #{i+1} 次times循环"
end

#step循环
1.step(10,3) do |i|
puts "#{i}"
end

循环处理

知识点

  • for
  • while
  • until

实战演习

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
#循环数组
gamelist = ["塞尔达传说", "超级马里奥", "开心剪纸"]
for game in gamelist do
puts game
end

#循环1-5
for num in 1..5 do
puts num
end

#循环1-4
for num in 1...5 do
puts num
end

#while循环
index = 0
while index < 5 do
puts "while.index=" + index.to_s
index+=1
end

#untile
index = 0
until index == 5 do
puts "until.index=" + index.to_s
index+=1
end

条件控制

知识点

  • if/else
  • if/elsif
  • unless
  • case/when

实战演习

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
#---------------#
# NBA球员工资估算
#---------------#
point_per_game = 15

if point_per_game >= 30
puts "3500万美元/年"
elsif point_per_game >= 20
puts "2000万美元/年"
else
puts "中产合同吧"
end

#---------------#
# 买这个游戏吗?
#---------------#
#《绝地求生》(PUBG)
PUBG_SteamPrice = 40

#unless=只要不
unless PUBG_SteamPrice < 50
#大于等于50的时候
puts "《绝地求生》这个游戏虽然好玩,但是价格太贵,我还是玩学习版吧。"
else
#小于50的时候
puts "《绝地求生》降价了,大家要支持正版啊!剁手买!"
end

#---------------#
# 今天是星期几?
#---------------#
week_day = 0

case week_day
when 0,7
puts "星期日"
when 1
puts "星期一"
when 2
puts "星期二"
when 3
puts "星期三"
when 4
puts "星期四"
when 5
puts "星期五"
when 6
puts "星期六"
else
raise Exception.new("没这天")
end

模块的定义

Ruby模块其实类似Class类的概念,但也有所不同,今天我们来学习一下吧。

知识点

  • Ruby模块的定义

实战演习

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
module BaseFunc
Version = "0.0.1"

def v
return Version
end

def add(a, b)
return a + b
end

def self.showVersion
return Version
end

#将v方法定义范围静态方法
module_function :v
end

puts BaseFunc::Version
puts BaseFunc.showVersion
puts BaseFunc::showVersion
puts BaseFunc.v
#puts BaseFunc.add(10, 30)

class BaseClass include BaseFunc
end

puts "=============================="
puts BaseClass::Version
# puts BaseClass.showVersion
# puts BaseClass.v

myCls = BaseClass.new
puts myCls.add(10,20)

Class类继承

类继承是面向对象编程的基础,今天我们来讲解一下Ruby的类继承。

知识点

  • Ruby实现类继承

实战演习

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
class Game
def initialize(id, title, price)
@id = id
@title = title
@price = price
end

def showGame
puts @id + ", " + @title + ", " + @price.to_s
end

def self.toStr
puts "I love this game."
end
end

class SteamGame < Game
def SteamInfo
puts "G胖说了,STEAM要统一各个平台,完成Game All In One。"
end
end

SteamGame.toStr

mygame = SteamGame.new("nobunaga-taishi", "信長の野望・大志", 450)
mygame.showGame
mygame.SteamInfo

Class再入门

知识点

  • Ruby类再入门

实战演习

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Game
def initialize(id, title, price)
@id = id
@title = title
@price = price
end

def showGame
puts @id + ", " + @title + ", " + @price.to_s
end

def self.toStr
puts "I love this game."
end
end

zelda = Game.new("zelda", "ゼルダの伝説", 350)
zelda.showGame
#常常错误的滋味
#zelda.toStr

Game.toStr
Game::toStr

类型转换

知识点

  • 字符to数值
  • 数值to字符

实战演习

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#180俱乐部计划
FG = "50" #投篮命中率
P3 = "40" #三分命中率
FT = "90" #罚球命中率

puts FG + P3 + FT
puts FG.to_i + P3.to_i + FT.to_i
puts (FG.to_i + P3.to_i + FT.to_i).to_s + " 俱乐部"

#整数2小数
puts FG.to_i
puts FG.to_i.to_f

#小数2整数
puts 1234.5678
puts 1234.5678.to_i

哈希变量

知识点

  • 定义使用哈希变量(key-value值对)

实战演习

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
mvp_rank = {
"curry" => 28.1,
"harden" => 30.3,
"lebron" => 26
}

puts mvp_rank
puts mvp_rank["harden"]

# 类似JSON使用
player = {
name: "harden",
age: 28,
point: 30.3
}

puts player
puts player[:name] + ", " + player[:age].to_s + ", " + player[:point].to_s