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
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
flyerhzm
September 19, 2014
Programming
5
3.7k
Write ruby code to change ruby code
flyerhzm
September 19, 2014
Tweet
Share
More Decks by flyerhzm
See All by flyerhzm
玩转 AST
flyerhzm
0
330
Find and Replace Code based on AST
flyerhzm
0
450
Rails Performance Tips
flyerhzm
0
1.3k
Refactor ruby code based on AST
flyerhzm
4
4.2k
基于AST的代码优化
flyerhzm
10
890
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
Unity6.3 AudioUpdate
cova8bitdots
0
140
エンジニアの「手元の自動化」を加速するn8n 2026.02.27
symy2co
0
160
CS教育のDX AIによる育成の効率化
niftycorp
PRO
0
130
社内規程RAGの精度を73.3% → 100%に改善した話
oharu121
13
8.1k
Claude Code の Skill で複雑な既存仕様をすっきり整理しよう
yuichirokato
1
400
Fundamentals of Software Engineering In the Age of AI
therealdanvega
1
260
米国のサイバーセキュリティタイムラインと見る Goの暗号パッケージの進化
tomtwinkle
2
600
Codex の「自走力」を高める
yorifuji
0
1.2k
Vuetify 3 → 4 何が変わった?差分と移行ポイント10分まとめ
koukimiura
0
150
コーディングルールの鮮度を保ちたい / keep-fresh-go-internal-conventions
handlename
0
200
PostgreSQL を使った快適な go test 環境を求めて
otakakot
0
560
grapheme_strrev関数が採択されました(あと雑感)
youkidearitai
PRO
1
230
Featured
See All Featured
We Are The Robots
honzajavorek
0
200
Pawsitive SEO: Lessons from My Dog (and Many Mistakes) on Thriving as a Consultant in the Age of AI
davidcarrasco
0
88
Designing Powerful Visuals for Engaging Learning
tmiket
0
280
The Cult of Friendly URLs
andyhume
79
6.8k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.4k
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
150
Unlocking the hidden potential of vector embeddings in international SEO
frankvandijk
0
200
What's in a price? How to price your products and services
michaelherold
247
13k
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
470
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
110
Measuring Dark Social's Impact On Conversion and Attribution
stephenakadiri
1
160
Done Done
chrislema
186
16k
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