自动化打包之fastlane--(6) ruby使用小结

自动化打包之fastlane–(1) 研究之必须提前了解的几点

自动化打包之fastlane–(2) fastlane init图文教程

自动化打包之fastlane–(3) 安装fir插件图文教程

自动化打包之fastlane–(4) 安装其他插件

自动化打包之fastlane–(5) 自动打包到蒲公英

自动化打包之fastlane–(6) ruby使用小结

自动化打包之fastlane–(7) 常用actons操作

自动化打包之fastlane–(8) 代码签名和项目配置

自动化打包之fastlane–(9) 常见错误

fastlane是用Ruby语言编写的一套自动化工具集和框架,每一个工具实际都对应一个Ruby脚本

1. 输出

puts "Hello, Ruby!";

2. 数据类型

  • Ruby支持的数据类型包括基本的Number、String、Ranges、Symbols,以及true、false和nil这几个特殊值,同时还有两种重要的数据结构——Array和Hash。
a = 1
b = 2.1
c = "string"

//数组

//初始化赋值
arr = [1, 1.1, "string"]
//遍历
arr.each do |i|
    puts i
end

//hash

dictHash = ["name" => "zhouyu", "age" => 18, "height" => 174.00]
//遍历
dictHash.each do |key, value|
   print key,value
end

//Range

  • 范围 (1..5) 意味着它包含值 1, 2, 3, 4, 5,范围 (1…5) 意味着它包含值 1, 2, 3, 4
(10..15).each do |i|
    print i
end

//Symbols

  • 在 Ruby 中 Symbol 表示“名字”,比如字符串的名字,标识符的名字。
  • 其实Symbol是一种非常简单的Object
  • Symbol是一个object,但是他可以用number表示也可以用string表示,多数情况下Symbol是作为一个字符串展现的。

3. #{ expr }

  • 您可以使用序列 #{ expr } 替换任意 Ruby 表达式的值为一个字符串。在这里,expr 可以是任意的 Ruby 表达式。
name = "zhouyu"
puts "#{name+", ok"}"

4. 类和对象

//定义
calss Person 
end
//定义
class Person {
  String name
  Number age

  Function running {

  }
}
//创建
person = Person.new
//自定义方法创建
class Person 
  def initialize (id, name, age)
    @cust_id = id
    @cust_name = name
    @cust_age = age
  end
end

person = Person.new("1", "zhouyu", "27")

5. 变量

  • 局部变量:局部变量是在方法中定义的变量。局部变量在方法外是不可用的。在后续的章节中,您将看到有关方法的更多细节。局部变量以小写字母或 _ 开始。
  • 实例变量:实例变量可以跨任何特定的实例或对象中的方法使用。这意味着,实例变量可以从对象到对象的改变。实例变量在变量名之前放置符号(@)。
  • 类变量:类变量可以跨不同的对象使用。类变量属于类,且是类的一个属性。类变量在变量名之前放置符号(@@)。
  • 全局变量:类变量不能跨类使用。如果您想要有一个可以跨类使用的变量,您需要定义全局变量。全局变量总是以美元符号($)开始。

//

Ruby 支持五种类型的变量。
1. 一般小写字母、下划线开头:变量(Variable)。
2. $开头:全局变量(Global variable)。
3. @开头:实例变量(Instance variable)。
4. @@开头:类变量(Class variable)类变量被共享在整个继承链中
5. 大写字母开头:常数(Constant)。

//

Ruby 伪变量
它们是特殊的变量,有着局部变量的外观,但行为却像常量。您不能给这些变量赋任何值。
- self: 当前方法的接收器对象。
- true: 代表 true 的值。
- false: 代表 false 的值。
- nil: 代表 undefined 的值。
- __FILE__: 当前源文件的名称。
- __LINE__: 当前行在源文件中的编号。

6. 成员函数

  • 在 Ruby 中,函数被称为方法。类中的每个方法是以关键字 def 开始,后跟方法名。
  • 方法名总是以小写字母开头。在 Ruby 中,您可以使用关键字 end 来结束一个方法。
calss Person 
    def run
        puts "跑步"
    end
    def sleep
        puts "睡觉"
    end
end

person = Person.new
person.run
person.sleep

7. 条件判断

if语句

if x > 8 then puts "true" end

if x > 0 
   puts "x > 0"
elseif x > 8 and x != 9
   puts "x > 8 and x != 9"
else
   puts "其他"
end

case语句

$age =  5
case $age
when 0 .. 2
    puts "婴儿"
when 3 .. 6
    puts "小孩"
when 7 .. 12
    puts "child"
when 13 .. 18
    puts "少年"
else
    puts "其他年龄段的"
end

8. 循环

while

$i = 0
$num = 5

while $i < $num  do
   puts("在循环语句中 i = #$i" )
   $i +=1
end

begin
   puts("在循环语句中 i = #$i" )
   $i +=1
end while $i < $num

for

for i in 0..5
   puts "局部变量的值为 #{i}"
end

break

for i in 0..5
   if i > 2 then
      break
   end
   puts "局部变量的值为 #{i}"
end

next 类似其他语言的continue

for i in 0..5
   if i < 2 then
      next
   end
   puts "局部变量的值为 #{i}"
end

9. 方法

方法

//简写
def mathod
end
//带参数
def mathod_name (arg1, agr2)
    puts __LINE__
end
//参数有默认值
def mathod_name (arg1 = default1, agr2 = default2)
    puts __LINE__
end
//有返回值
def mathod_name (arg1, agr2)
    puts __LINE__
    return "result"
end
//可变参数
def mathod_name (*arg1)
    puts __LINE__
    return "result"
end

10. 块

  • 块中的代码总是包含在大括号 {} 内。
  • 块总是从与其具有相同名称的函数调用。这意味着如果您的块名称为 test,那么您要使用函数 test 来调用这个块。
  • 您可以使用 yield语句来调用块。

语法

block_name{
   statement1
   statement2
   ..........
}

调用

//yield调用
def test
   puts "在 test 方法内"
   yield #调用了一次块
   puts "你又回到了 test 方法内"
   yield #调用了一次块
end
test {puts "你在块内"}

传递带有参数的 yield 语句

def test
   yield 5
   puts "在 test 方法内"
   yield 100
end
test {|i| puts "你在块 #{i} 内"}

BEGIN 和 END 块

BEGIN { 
  # BEGIN 代码块
  puts "BEGIN 代码块"
} 

END { 
  # END 代码块
  puts "END 代码块"
}
  # MAIN 代码块
puts "MAIN 代码块"

11. 模块(Module)

module Identifier
   statement1
   statement2
   ...........
end
# 定义在 trig.rb 文件中的模块

module Trig
   PI = 3.141592654
   def Trig.sin(x)
   # ..
   end
   def Trig.cos(x)
   # ..
   end
end
$LOAD_PATH << '.'

require 'trig.rb'

y = Trig.sin(Trig::PI/4)

12. Ruby 中的 Mixins

  • Ruby 没有真正实现多重继承机制,而是采用成为mixin技术作为替代品。将模块include到类定义中,模块中的方法就mix进了类中。
module A
   def a1
   end
   def a2
   end
end
module B
   def b1
   end
   def b2
   end
end

class Sample
include A
include B
   def s1
   end
end

samp=Sample.new
samp.a1
samp.a2
samp.b1
samp.b2
samp.s1

13. 迭代器

each 迭代器

ary = [1,2,3,4,5]
ary.each do |i|
   puts i
end

collect 迭代器

a = [1,2,3,4,5]
b = a.collect{|x| 10*x}
puts b
sum = 0
cutcome = {"block1" => 1000, "book2" => 1000, "book3" => 4000}
cutcome.each{|item, price| sum += price}
print "sum = " + sum.to_s

sum = 0
cutcome = {"block1" => 1000, "book2" => 1000, "book3" => 4000}
cutcome.each{|pair| sum += pair[1]}
print "sum = " + sum.to_s

14. 类

  • Ruby 中的一切都是以对象的形式出现。Ruby 中的每个值都是一个对象,即使是最原始的东西:字符串、数字,甚至连 true 和 false 都是对象。类本身也是一个对象,是 Class 类的一个实例。

定义

class Box
   code
end

创建对象

box1 = Box.new
box2 = Box.new

initialize 方法

class Box
   def initialize(w,h)
       // 给实例变量赋值
      @width, @height = w, h
   end
end

访问器(getter) & 设置器(setter)方法

# 定义类
class Box
   # 构造函数
   def initialize(w,h)
      @width, @height = w, h
   end

   # 访问器方法
   def printWidth
      @width
   end

   def printHeight
      @height
   end
end

# 创建对象,初始化盒子的高度与宽度
box = Box.new(10, 20)

# 使用访问器方法
x = box.printWidth()
y = box.printHeight()

puts "盒子宽度 : #{x}"
puts "盒子高度 : #{y}"

类方法 & 类变量

  • 类变量是在类的所有实例中共享的变量。换句话说,类变量的实例可以被所有的对象实例访问。类变量以两个 @ 字符(@@)作为前缀,类变量必须在类定义中被初始化,如下面实例所示。

  • 类方法使用 def self.methodname() 定义,类方法以 end 分隔符结尾。类方法可使用带有类名称的 classname.methodname 形式调用,

class Box
   # 初始化类变量
   @@count = 0
   def initialize(w,h)
      # 给实例变量赋值
      @width, @height = w, h

      @@count += 1
   end

   def self.printCount()
      puts "Box count is : #@@count"
   end
end

# 创建两个对象
box1 = Box.new(10, 20)
box2 = Box.new(30, 100)

# 调用类方法来输出盒子计数
Box.printCount()

to_s 方法

  • 定义的任何类都有一个 to_s 实例方法来返回对象的字符串表示形式
class Box
   # 构造器方法
   def initialize(w,h)
      @width, @height = w, h
   end
   # 定义 to_s 方法
   def to_s
      "(w:#@width,h:#@height)"  # 对象的字符串格式
   end
end

# 创建对象
box = Box.new(10, 20)

# 自动调用 to_s 方法
puts "String representation of box is : #{box}"

类的继承

  • Ruby 不支持多继承,但是 Ruby 支持 mixins。mixin 就像是多继承的一个特定实现,在多继承中,只有接口部分是可继承的。
# 定义类
class Box
   # 构造器方法
   def initialize(w,h)
      @width, @height = w, h
   end
   # 实例方法
   def getArea
      @width * @height
   end
end

# 定义子类
class BigBox < Box

   # 添加一个新的实例方法
   def printArea
      @area = @width * @height
      puts "Big box area is : #@area"
   end
end

# 创建对象
box = BigBox.new(10, 20)

# 输出面积
box.printArea()

类常量

  • 可以在类的内部直接访问常量,就像是访问变量一样,但是如果您想要在类的外部访问常量,那么您必须使用 classname::constant
# 定义类
class Box
   BOX_COMPANY = "TATA Inc"
   BOXWEIGHT = 10
   # 构造器方法
   def initialize(w,h)
      @width, @height = w, h
   end
   # 实例方法
   def getArea
      @width * @height
   end
end

# 创建对象
box = Box.new(10, 20)

# 调用实例方法
a = box.getArea()
puts "Area of the box is : #{a}"
puts Box::BOX_COMPANY
puts "Box weight is: #{Box::BOXWEIGHT}"

类信息

  • 用法和Objective,SwIft类似,也类似Java和JavaScript中的this
class Box
   # 输出类信息
   puts "Class of self = #{self.class}"
   puts "Name of self = #{self.name}"
end

参考:
Ruby 菜鸟教程

猜你喜欢

转载自blog.csdn.net/kuangdacaikuang/article/details/80460992