ruby学习笔记
ruby学习
初识类
# @name是实例变量,能够被该类或者子类继承使用
class Player
def initialize(name = "Koma")
@name = name
end
def show()
puts "player: #{@name}"
end
end
koma = Player.new()
koma.show()
curry = Player.new("Curry")
curry.show()
harden = Player.new("Harden")
harden.show()
class Game
attr_accessor :price, :title #定义可存取对象的属性
def initialize(title = "怪物猎人世界", price = 200)
@title = title
@price = price
end
def show()
puts "标题: #{@title}"
puts "价格: #{@price}"
end
end
mygame = Game.new()
mygame.show()
puts "title is " + mygame.respond_to?("title").to_s
puts "price is " + mygame.respond_to?("price").to_s
mygame.title = "Super Mario World"
mygame.price = 150
mygame.show()
class Game
def initialize(title = "怪物猎人世界", price = 200)
@title = title
@price = price
end
def show()
puts "标题: #{@title}"
puts "价格: #{@price}"
end
def show2()
end
def show3()
end
end
puts Game.instance_methods(false) #false打印定义的方法
mario = Game.new("超级马里奥", 350)
if mario.respond_to?("show")
mario.send("show")
end
循环和join的使用
games = ["绝地逃生", "怪物猎人世界","信长之野望大志"]
puts games
games.each do |game|
puts "我爱<<#{game}>>"
end
games.each_with_index do |game,i|
puts "#{i+1}.#{game}"
end
puts games.join(",")
定义使用哈希变量
mvp_rank = {
"curry" => 28.1,
"harden" => 30.3,
"lebron" => 26
}
puts mvp_rank
puts mvp_rank["harden"]
player = {
name: "harden",
age: 28,
point:30.3
}
puts player
puts player[:name] + ", " + player[:age].to_s + ", " + player[:point].to_s
类1
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", "Harden", 330)
zelda.showGame
Game.toStr
Game::toStr#使用类调用静态方法
关于继承
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", "关云长", 110)
mygame.showGame #输出父类方法
mygame.SteamInfo
关于模块
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
class BaseClass include BaseFunc
end
puts "================================="
puts BaseClass::Version
# puts BaseClass.showVersion
# puts BaseClass.v #模块中方法被定义为静态时,类不能直接使用
myCls = BaseClass.new #实例方法通过类实例后可以使用
puts myCls.add(10,20) #只要是静态方法、不实例化 类直接引用的方法,那么它是不能够继承模板的
例外处理
begin
puts ">处理开始"
10/0
rescue => e
#错误发生时
puts "X错误发生!"
puts e
else
#正常处理
puts "O正常处理"
ensure
#最后处理,无论是否发生处理(final)
puts "_最后的扫尾处理"
end
【华为OD机试真题】可以转到CSDN相关专栏订阅学习:https://blog.csdn.net/weixin_45541762/article/details/129903356
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】