目录
常量
# 常量
Name = "xiaoming"
# 变量
age = 18
# 全局变量
$all = "hello world"
变量和数据类型
一、基础类型
bl = false
num = 18
str = "hello wolrd"
array = [1, 2, 3]
hash = {name: "xiaoxing", age: 18}
二、方法
# 方法
def testfn name, age
puts "#{name},#{age}"
end
testfn "xiaoming", 18
# lambda 表达式
lmd = lambda do |param|
puts "param is #{param}"
end
lmd.call "xiaoming"
三、对象
# 对象
class Person
# 会生成getter和setter方法
attr_accessor :name
attr_accessor :age
# 构造函数
def initialize name, age
@name = name
@age = age
end
# 实例方法
# 类方法
# 封装 继承 多肽
end
分支结构
testNum = 10
if testNum > 10
puts "大于10"
elsif testNum > 5
puts "大于5"
else
puts "小于5"
end
case testNum
when 10
puts "等于10"
else
puts "未匹配"
end
循环结构
for i in 1..5 do
puts "i = #{i}"
end
counter = 0
while counter < 10 do
counter = counter + 1
puts "counter = #{counter}"
end
行者常至,为者常成!