Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
ruby新手读源码.pdf
Search
覃明圆
January 20, 2015
1
280
ruby新手读源码.pdf
覃明圆
January 20, 2015
Tweet
Share
More Decks by 覃明圆
See All by 覃明圆
思考middleware
qinmingyuan
0
190
Featured
See All Featured
How GitHub (no longer) Works
holman
314
140k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
46
9.6k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
8
800
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
233
17k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
161
15k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
17
950
Statistics for Hackers
jakevdp
799
220k
Building Adaptive Systems
keathley
43
2.6k
Being A Developer After 40
akosma
90
590k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Practical Orchestrator
shlominoach
188
11k
Transcript
Ruby新⼿手读源码 覃明圆 薄荷科技
读源码style A. 通读源码(⽼老⼿手) B. 通过跟踪程序运⾏行栈读源码(新⼿手) C. 通过查bug读源码(新⼿手)
内省篇 introspection
None
• 说明: • 所有代码都在ruby 2.2版本正常⼯工作,2.2之前版 本有些⽅方法没有实现或实现有所差别。 • 由于时间关系,这部分代码不作演⽰示,建议⼤大家 打开console参照ppt⾃自⾏行实验。
对象 Object
1. 实例变量 • obj.instance_variables() 2-1. 所属的类 • obj.class() 2-2. 所属的本类
• obj.singleton_class()
类(class)
1. 继承信息、include 的模块 • klass.superclass() • klass.included_modules() • klass.ancestors() 2.
类变量 • klass.class_variables() 3. 定义的实例⽅方法 • klass.instance_methods()
变量(variables) 与 常量(constant)
• 实例变量属于对象(类也是对象) • obj.instance_variables()
• 类变量属于类体系结构(类,⼦子类,实例) • klass.class_variables() • sub_klass.class_variables()
• 局部变量属于作⽤用域 • local_variables()
• 全局变量属于全局 • global_variables()
常量 1. 所有顶级常量 • Module.constants() 2. 当前范围内的常量 • Module#constants()
⽅方法(method)
对象的⽅方法 • obj.methods() • obj.methods(false) • obj.singleton_methods()
⽅方法的可⻅见性 • obj.public_methods() • obj.protect_methods() • obj.private_methods()
• 获得⼀一个 method 对象 • obj.method(:require)
• 源码位置 • obj.method(:require).source_location
method 对象更多⽅方法 • meth.call • meth.receiver • meth.super_method
UnboundMethod • meth.unbind 转化为 UnboundMethod对象
• UnbondMethod 对象没有接收者(receiver) • unbind_meth.bind(obj) 转化为 Method 对象
⼯工具篇 tools
⼯工具提升⽣生产⼒力
• pry • pry-byebug(byebug) • pry-rails • pry-stack_explorer • pry-rescue
• web-console
• binding.pry 进⼊入runtime session
pry (1) • ls 列出当前的变量和⽅方法 • cd 进⼊入特定的程序上下⽂文环境 • whereami
当前context所在位置 • watch 跟踪某个变量,发⽣生变化时打印出来
pry (2) • show-source 查看⽅方法或者类定义的位置 • stat 查看⽅方法的信息
pry (3) • nesting 跟踪pry进⼊入的上下⽂文嵌套信息
byebug • step: ⼀一步步执⾏行下⼀一⾏行命令,或进⼊入下⼀一个⽅方 法。
pry-stack_explorer • show-stack:查看程序的运⾏行栈,深⼊入了解程序 运⾏行的过程 • frame:⾄至特定的frame • down / up:
跳转⾄至上⼀一个或下⼀一个frame
pry-rescue • rescue 命令 • cd-cause:Move to the exception that
caused this exception to happen • try-again:Re-try the code that caused this exception
web-console • 在浏览器提供 命令⾏行界⾯面 • 通过 console 调⽤用或者 异常 ⾃自动调⽤用
联系⽅方式:
[email protected]