Ruby & Cocoapods
原理、脚本、组件
- Python的解析器实现更成熟,第三方库质量高。但是Ruby包管理更简单,方便
- Python的应用领域广泛.而Ruby目前主要局限在web领域与精致项目
- Python语法简单,ruby更抢单,灵活
安装了Cocoapods的,就必然按钮了Ruby, 查看是否安装: ruby --version
1. 安装篇
- rvm & rbenv是⼀种命令⾏⼯具,可让您轻松地安装,管理和使⽤多个Ruby环境。
- 这两个⼯具本质都是 PATH 上做⼿脚,⼀个在执⾏前,⼀个在执⾏中。 如果你不需要维护特定版本的 Ruby 项⽬,那么只需要装⼀个⽐较新的 Ruby 版本就⾏了。
brew install ruby
2. Ruby gem命令
与⼤多数的编程语⾔⼀样,Ruby 也受益于海量的第三⽅代码库。
- 这些代码库⼤部分都以 Gem 形式发布。 RubyGems 是设计⽤来帮助创建,分享和安装 这些代码库的。
- gem search -r/-f
- gem install --version
- gem list
启动ruby
启动ruby:irb
打印: puts "hello world"
退出: quit()
3. 调试
Ruby ruby-debug-ide gem
- ruby-debug-ide提供了交互环境。和IDE(例如RubyMine,Visual Studio Code或Eclipse)之间建⽴通信的协议。“ ruby -debug-ide”将命令从IDE重定向到调试器。然后,它将从调试器收到的答案/事件返回给IDE。
gem install ruby-debug-ide
报错使用 sudo gem install ruby-debug-ide sudo是获取管理员权限,输入该命令便可继续执行了。
debase
- debase是针对Ruby 2.0的标准Ruby调试器debug.rb的快速实现。它是通过利⽤新的Ruby TracePoint类实现的。核⼼组件提供了前端可以建⽴的⽀持。它提供断点处理, 堆栈信息等。
gem install debase
注释
# 注释
这是一个多行注释。 可扩展至任意数量的行。 但 =begin 和 =end 只能出现在第一行和最后一行。 如
=begin
cat = 'Cat1234'
=end
01-Rudy注释&打印
# 单行注释
# 这是一个多行注释。
# 可扩展至任意数量的行。
# 但 =begin 和 =end 只能出现在第一行和最后一行。
cat = 'Cat1234'
puts "Logic #{cat}"
puts 'Logic #{cat}'
puts 'Cat1237'
print 'Cat1237'
print 'Cat1237'
puts `ruby --version`
02-Rudy对象&符号&变量
# 万物皆是对象
puts 3.class
puts '3'.class
puts nil.class
puts true.class
puts false.class
cat = 'Cat1237'
puts cat.to_s
# q 使用的是单引号引用规则,而 %Q 是双引号引用规则
puts cat.to_s
puts %q(#{cat})
# 在一个双引号括起的字符串内,转义字符会被解析。在一个单引号括起的字符串内,转义字符不会被解析,原样输出。
puts %q(I can \n。)
puts %(I can \n。)
hank = cat = 'Cat1237'
str = 'Won\'t you read O\'Reilly\'s book?'
puts str
# ruby symbol ruby c 结构体 symbol table
:cat
puts :cat.class
# symbol唯一的
c = :cat
puts c == :cat
puts c == :cat
puts c.to_s
puts 'cat'.to_sym
puts c.object_id
puts :cat.object_id
puts 'cat'.object_id
puts 'cat'.object_id
puts 'cat'.object_id
puts "Hello".method(:class).class
03-Rudy中的操作符
puts 1 + 1 #=> 2
puts 8 - 1 #=> 7
puts 10 * 2 #=> 20
puts 35 / 5 #=> 7
puts 2**5 #=> 32
puts 5 % 3 #=> 2
puts 3 & 5 #=> 1
puts 3 | 5 #=> 7
puts 3 ^ 5 #=> 6
puts 3.+ 3
puts !!nil #=> false
puts !!false #=> false
puts !!0 #=> true
puts !!"" #=> true
# 组合比较运算符
# 返回1,第一个参数大
# 返回0,两个参数相等
# 返回-1,右边参数大
puts 1 <=> 10
puts 10 <=> 1 #=> 1 (10 > 1)
puts 1 <=> 1 #=> 0 (1 == 1)
# and 左侧执行成功,继续执行右侧
(1 < 0) and (puts "mm")
# or 左侧执行失败,继续执行右侧
(1 < 0) or (puts "cc")
04-Rudy中的String
cat = 'cat'
# utf-8
puts cat.encoding.name
# 非破坏性
newStr = cat.encode!('ISO-8859-1')
puts cat.encoding.name
# 破坏性
cat.force_encoding('ISO-8859-1')
puts cat.encoding.name
newStr.each_byte do |byte|
puts byte
end
newStr.each_byte { | byte |
puts byte
}
# 拼接字符串
puts 'hello ' + 'world' #=> "hello world"
puts 'hello ' + 3.to_s #=> "hello 3"
# 拼接一个对象
puts 'hello' << ' world'
puts 'hello' << newStr
# 重复输出指定字符串n次
puts 'hello ' * 3 #=> "hello hello hello "
# =~ 返回匹配到到字符的索引(如果没有匹配,则返回nil)
puts 'Cat' =~ /[t]/
# 格式化字符串
puts format('%05d', 123)
puts format("%-5s: %08x #{cat}", 'ID', 44)
# 首字母大写
puts cat.capitalize
# ? !操作符 不一样的
# ?返回的是一个 布尔值
puts cat.empty?
puts "123" == "123"
# !破坏性
puts "123".eql?("123")
# "cat"
puts cat.sub(/[a]/, "b")
sleep 1
05-Rudy中的分支判断&循环&集合类型
array = [1, 2, 3, 4, 5]
# u数组可以包含多种数据类型
[1, 'hello', false]
# %r() 是写正则表达式的另一种方式。
# %q() 是写单引号字符串的另一种方式(可以是多行,这很有用)
# %Q() 给出双引号字符串
# %x() 是一个shell命令
# %i() 给出一个符号数组(Ruby> = 2.0.0)
# %s()变成foo符号(:foo)
# %w写一个用空格而不是逗号分隔且不带引号的字符串数组
puts %w[1 2 3]
puts array[0] #=> 1
puts array.first #=> 1
puts array[12] #=> nil
puts array[-1] #=> 5
puts array.last #=> 5
# 指定 index 和 length...
puts array[2, 3] #=> [3, 4, 5]
# 指定range
puts array[1..3] #=> [2, 3, 4]
# 逆转数组
puts [1, 2, 3].reverse #=> [3,2,1]
# 万物皆是对象
puts array.[] 0
# 添加到数组
array << 6 #=> [1, 2, 3, 4, 5, 6]
array.push(6) #=> [1, 2, 3, 4, 5, 6]
# 判断数据是否存在?
puts array.include?(1) #=> true
# dictionary
hash = { 'color' => 'green', 'number' => 5 }
hash.keys #=> ['color', 'number']
hash['color'] #=> "green"
hash['number'] #=> 5
#sys
hash = { :defcon => 3, :action => true }
hash.keys #=> [:defcon, :action]
hash = { defcon: 3, action: true }
hash.keys #=> [:defcon, :action]
# 是否存在key和value
hash.key?(:defcon) #=> true
hash.value?(3) #=> true
if true
puts 'if statement'
elsif false
puts 'else if, optional'
else
puts 'else, also optional'
end
warnings = ['Patronimic is missing', 'Address too short']
puts("Some warnings occurred:\n" + warnings.join("\n")) if !warnings.empty?
puts("Some warnings occurred:\n" + warnings.join("\n")) unless warnings.empty?
# 循环
# 在ruby中,for循环不常见
for counter in 1..5
puts "iteration #{counter}"
end
# block 闭包
(1..5).each do |counter|
puts "iteration #{counter}"
end
(1..5).each { |counter|
puts "iteration #{counter}"
}
# 需要index和value同时存在
array.each_with_index do |element, index|
puts "#{element} is number #{index} in the array"
end
counter = 1
while counter <= 5 do
puts "iteration #{counter}"
counter += 1
end
# 'map', 'reduce'等
array = [1,2,3,4,5]
#return
doubled = array.map { |element|
element * 2
}
a = ["FOO", "BAR", "BAZ"]
a.map { |s| s.downcase }
# 通过&符号控制对每个元素执行等方法
a.map(&:downcase)
upcased = ['Watch', 'these', 'words', 'get', 'upcased'].map(&:upcase)
sum = [1, 2, 3, 4, 5].reduce(&:+)
puts sum
# 分支
grade = 'B'
case grade
when 'A'
puts 'Way to go kiddo'
when 'B'
puts 'Better luck next time'
when 'C'
puts 'You can do better'
when 'D'
puts 'Scraping through'
when 'F'
puts 'You failed!'
else
puts 'Alternative grading system, eh?'
end
# 分支和range
grade = 82
case grade
when 90..100
puts 'Hooray!'
when 80...90
puts 'OK job'
else
puts 'You failed!'
end
06-Rudy中的异常处理
# Exception handling
begin
# Code here that might raise an exception
raise NoMemoryError, '内存异常.'
rescue RuntimeError => other_exception_variable
puts 'RuntimeError异常'
else
puts '没有异常'
ensure
puts '不管有没有异常'
end
07-Rudy中的方法与闭包
# Methods
# 没有return关键字,传递参数可以使用括号,也可以不用
def double(x)
x * 2
end
puts double(2) #=> 4
puts double 3 #=> 6
puts double double 2
def sum(x, y)
x + y
end
# 多参数通过,分割
sum 3, 4 #=> 7
puts sum sum(3, 4), 5 #=> 12
# yield
# 传递一个block
def logic
puts '{'
yield
puts '}'
end
logic { puts 'Cat1 1237' }
# Block可以被包装成一个`Proc`对象
# &
def guests(&block)
puts block.class #=> Proc
puts block.call(4)
end
guests { |n| puts "Cat --#{n}" }
# 传递一组参数
def guess(*arr)
arr.each { |guest| puts guest }
end
guess %w[Cat Kody cooci]
# 根据数组自动创建对象
a = 'Cat'
b = 'Kody'
c = 'Cooci'
puts a
puts b
puts c
\
cat = %w[C g b m o]
def get(first, second, third)
puts "#{first}---#{second}---#{third}"
end
# 通过*获取指定数组
get(*cat.first(3))
def get1(first, second, third, *other)
puts "#{first}---#{second}---#{third}"
puts other.count.to_s
end
get1(*cat)
cat = 'Cat'
puts cat.upcase
puts cat.upcase!
puts cat
08-Rudy中的类与继承
class Logic
@@species = 'Cat123'
def initialize(name, age = 0)
@name = name
@age = age
end
attr_accessor :name
def self.say(n)
puts n
end
def speec
puts 'Kody'
end
def self.species
puts @@species
end
end
lg = Logic.new('cat')
puts Logic.species
Logic.say('mm')
lg.speec
# $ 全局变量
$var = "I'm a global var"
puts defined? $var #=> "global-variable"
# 实例变量
@var = "I'm an instance var"
puts defined? @var #=> "instance-variable"
# 类实例变量
@@var = "I'm a class var"
puts defined? @@var #=> "class variable"
# 以大写字母开头的变量是常量。
Var = "I'm a constant"
puts defined? Var #=> "constant"
# 类变量在类及其所有后代之间共享
class Logic
@@species = 'Cat123'
@cooci = '123'
def initialize(name, age = 0)
@name = name
@age = age
end
attr_accessor :name
def self.say(_n)
puts @cooci
end
def speec
puts 'Kody'
end
def self.species
puts @@species
end
end
# 派生类
class Cat < Logic
end
Cat.species
Logic.species
# 实例变量不共享
class Kody < Logic
end
Logic.say('m')
Kody.say('n')
module ModuleExample
def foo
'foo'
end
end
# Including modules 绑定到类的实例
# Extending modules 绑定到类本身
class Person
include ModuleExample
end
# Person.foo
puts Person.new.foo