01. Ruby Basic
August 5, 2023About 2 min
Ruby console
irb
Hello world
puts "Hello world"
Function
basic function
# function without parameters
def hello
puts "Hello world"
end
hello
parameter
Function with parametersClass
Define a class
Basic Class
class Player
def initialize(name="Haiyue")
@name=name
end
def show()
puts "Player: #{@name}"
end
end
obj = Player.new()
obj.show()
Static method
class Game
def initialize(id, title, price)
@id = id
@title = title
@price = price
end
def showGame()
puts @id + "," + @title + "," + @price.to_s
end
# static method: could be invoked with no need an instance
def self.toStr
puts "I love this game"
end
end
Game.toStr
Game::toStr
inherit
class Game
def initialize(id, title, price)
@id = id
@title = title
@price = price
end
def showGame()
puts @id + "," + @title + "," + @price.to_s
end
# static method: could be invoked with no need an instance
def self.toStr
puts "I love this game"
end
end
class SteamGame < Game
def SteamInfo
puts "child class class"
end
end
mygame = SteamGame.new('sfefsef', 'name', 1234)
SteamGame.toStr
methods
instance_methods
& respond_to?
- instance_methods: list all members inside of the class
- respond_to?: check whether a method or property is valid
instance_methods
class Player
def initialize(name="Haiyue")
@name=name
end
def show()
puts "Player: #{@name}"
end
end
# List all the member of a class
Player.instance_methods()
# List all user defined methods
Player.instance_methods(false)
respond_to?
class Player
def initialize(name="Haiyue")
@name=name
end
def show()
puts "Player: #{@name}"
end
end
# Check whether method show is valid outside class
obj = Player.new()
if obj.respond_to?("show")
obj.send("show")
end
attributes
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()
#check whether the attribute is avaliable outside class
puts "title is " + mygame.respond_to?("title").to_s
puts "title is " + mygame.respond_to?("price").to_s
# Please specify title as attr_accessor, then you could invoke it outside class
#mygame.title="New Game"
mygame.price=150
mygame.show()
Array
define an array
games = ["game1", "game2", "game3"]
puts games
iterate
each
games = ["game1", "game2", "game3"]
games.each do |game|
puts "Love #{game}"
end
each_with_index
games = ["game1", "game2", "game3"]
games.each_with_index do |game, i|
puts "Love #{i}, #{game}"
end
concatenate
games = ["game1", "game2", "game3"]
games.join(",")
whether is an array
if games.respond_to?("each")
Comments
one line comment
# comment here
multi line comment
=begin
comment here
=end
all comments after
__END__
All comments here
Comment 1
Comment 2
operator
basic operator
- + 加
- - 减
- * 乘
- ** 阶乘
- / 除法
- % 取余
compare operator
- ==
- !=
- >
- >=
- <
- <=
logical operator
- &&
- ||
- !
bit operator
- &
- |
- ^ 异或
- ~ 位反转
- <<
- >>
three item operator
condition ? operation1 : operation2
String operator
- + : concatenate
- << : append
- * : repeat
Difference of Quotation
" : parse the formula
' : original string
Hash
# similar with dictionary in python
# Hash variable
mvp_rank = {
"curry" => 28.1,
"harden" => 30.3,
"lebron" => 26
}
mvp_rank['curry']
# similar with JSON
player = {
name: "harden",
age: 28,
point: 30.3
}
puts player
player[:name]
Type Convert
FG = "50"
FG.to_i
FG.to_f
FG.to_f.to_s
References
Read to Number 17