Lock in $30 Savings on PRO—Offer Ends Soon! ⏳
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
ruby新手读源码.pdf
Search
覃明圆
January 20, 2015
1
310
ruby新手读源码.pdf
覃明圆
January 20, 2015
Tweet
Share
More Decks by 覃明圆
See All by 覃明圆
思考middleware
qinmingyuan
0
200
Featured
See All Featured
Building an army of robots
kneath
306
46k
Making Projects Easy
brettharned
120
6.5k
Code Review Best Practice
trishagee
74
19k
Why Our Code Smells
bkeepers
PRO
340
57k
Practical Orchestrator
shlominoach
190
11k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
54k
Designing Experiences People Love
moore
143
24k
Leading Effective Engineering Teams in the AI Era
addyosmani
8
1.3k
GraphQLの誤解/rethinking-graphql
sonatard
73
11k
Unsuck your backbone
ammeep
671
58k
Product Roadmaps are Hard
iamctodd
PRO
55
12k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.3k
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]