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
Write ruby code to change ruby code
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
flyerhzm
September 19, 2014
Programming
3.8k
5
Share
Write ruby code to change ruby code
flyerhzm
September 19, 2014
More Decks by flyerhzm
See All by flyerhzm
玩转 AST
flyerhzm
0
350
Find and Replace Code based on AST
flyerhzm
0
470
Rails Performance Tips
flyerhzm
0
1.4k
Refactor ruby code based on AST
flyerhzm
4
4.3k
基于AST的代码优化
flyerhzm
10
900
Building Asynchronous APIs
flyerhzm
25
9.1k
构建异步API服务
flyerhzm
19
7.4k
JRuby @ OpenFeint
flyerhzm
23
3.4k
Other Decks in Programming
See All in Programming
仕様漏れ実装漏れをなくすトレーサビリティAI基盤のご紹介
orgachem
PRO
9
5.1k
PCOVから学ぶコードカバレッジ #phpcon_odawara
o0h
PRO
0
240
Kubernetes上でAgentを動かすための最新動向と押さえるべき概念まとめ
sotamaki0421
3
440
Vibe하게 만드는 Flutter GenUI App With ADK , 박제창, BWAI Incheon 2026
itsmedreamwalker
0
540
Linux Kernelの1文字のミスで 権限昇格ができた話
rqda
0
2.3k
Coding at the Speed of Thought: The New Era of Symfony Docker
dunglas
0
4.7k
Nuxt Server Components
wattanx
0
260
Radical Imagining - LIFT 2025-2027 Policy Agenda
lift1998
0
250
AIと共にエンジニアとPMの “二刀流”を実現する
naruogram
0
130
「速くなった気がする」をデータで疑う
senleaf24
0
150
2026-03-27 #terminalnight 変数展開とコマンド展開でターミナル作業をスマートにする方法
masasuzu
0
310
煩雑なSkills管理をSoC(関心の分離)により解決する――関心を分離し、プロンプトを部品として育てるためのOSSを作った話 / Solving Complex Skills Management Through SoC (Separation of Concerns)
nrslib
3
760
Featured
See All Featured
Digital Ethics as a Driver of Design Innovation
axbom
PRO
1
260
jQuery: Nuts, Bolts and Bling
dougneiner
66
8.4k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
38
2.8k
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
210
Leo the Paperboy
mayatellez
7
1.6k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
5.9k
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
250
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
0
260
How to Think Like a Performance Engineer
csswizardry
28
2.5k
Marketing to machines
jonoalderson
1
5.1k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
880
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
54k
Transcript
Write ruby code to change ruby code Richard Huang Xinmin
Labs
About Me Founder at Xinmin Labs Full Stack Developer Open
Source Enthusiast @flyerhzm
Problem
Hey, let’s use rspec new syntax for project A Yes,
sure, I love the rspec new syntax
Hey, let’s use rspec new syntax for project B Hmmm…ok
Hey, let’s use rspec new syntax for project C Oh,
no, I don’t want to repeat it again
Developers love upgrading and refactoring code
Developers hate changing code again and again
Automation
Metric Tools
Metric Tools • cane - fails your build if code
quality thresholds are not met • reek - is a tool that examines Ruby classes, modules and methods and reports any code smells it finds. • rails_best_practices - is a tool to check the quality of rails code. • ……
Tell you what to do, but can’t do for you
IDE
IDE • can rename class / method / variable name
• can extract methods • can ……
IDE • can’t customize
Based on text
Based on text Replace FactoryGirl.create with create ! $ sed
-i '' 's/FactoryGirl.create/create/g' spec/**/*.rb
Based on text Replace FactoryGirl.create with create ! !
Based on text Hard to be accurate ! ! !
!
Based on text Hard to convert complicated cases ! !
! !
Based on AST
What’s AST AST is short for Abstract Syntax Tree
What’s AST
Why AST It is much more accurate to parse ruby
code.
None
None
How to use AST • ripper • ParseTree - https://github.com/seattlerb/parsetree
• ruby_parser - https://github.com/seattlerb/ruby_parser • parser - https://github.com/whitequark/parser
How to use AST
Example transpec - https://github.com/yujinakayama/ transpec
None
Based on AST Accurate, but hard to write and read
Synvert https://github.com/xinminlabs/synvert
Demo http://xinminlabs.github.io/synvert/
None
Synvert • Add useful attributes to AST nodes ! •
Provide DSL to parse and rewrite code
Attributes for AST node
Attributes for AST node
Attributes for AST node
Attributes for AST node Check out more attributes http://xinminlabs.github.io/synvert/rules/
DSL • within_file / within_files • with_node / within_node •
if_exist_node / unless_exist_node • append / insert / insert_after / replace_with / remove • ……
DSL Check out more DSL http://xinminlabs.github.io/synvert/dsl/
None
Playground http://synvert-tools.herokuapp.com/
Snippets https://github.com/xinminlabs/synvert-snippets
Want more? • Fork and send pull request • Open
issues on github • Hire us to refactor your project
Q&A ! Thank you