Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Write ruby code to change ruby code
Search
flyerhzm
September 19, 2014
Programming
3.9k
5
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Write ruby code to change ruby code
flyerhzm
September 19, 2014
More Decks by flyerhzm
See All by flyerhzm
玩转 AST
flyerhzm
0
490
Find and Replace Code based on AST
flyerhzm
0
610
Rails Performance Tips
flyerhzm
0
1.5k
Refactor ruby code based on AST
flyerhzm
4
4.4k
基于AST的代码优化
flyerhzm
10
980
Building Asynchronous APIs
flyerhzm
25
9.2k
构建异步API服务
flyerhzm
19
7.5k
JRuby @ OpenFeint
flyerhzm
23
3.7k
Other Decks in Programming
See All in Programming
Zero-Tap Sign-In対応、結局何をすればいいの?
mitohato14
0
1.8k
Distance field of Julia set
kinakomoti321
0
300
Kawaiicon: Kubernetes, it's no longer cool, but let's secure it anyway!
barnbarn
0
110
Long Living Tokens, Die, Die, Die! (or: The One Ring is Banned in Modern)
sylph01
0
410
AIがコードを書いてくれる時代、我々は何を確かめるのか
yosuke_furukawa
PRO
13
5.8k
우리의 Nitro Modules는 당신의 Turbo Modules보다 아름답다
l2hyunwoo
0
260
protojson における文字化けの扱い
kazuki1023
0
150
Spec-driven Development with AI
simas
PRO
0
2.3k
SFCで実現する病院・医療システムの動的組み立て式UI
koukimiura
1
170
オーケストレーション記述言語 Katari
yukikurage
0
240
[DroidKnights 2026] Workflow AX: From Auto-Documented Policies to E2E Testing
raeminkang
0
540
社内AIエージェント「Askワンバン」における コストチューニングの取り組み
t0yohei
0
160
Featured
See All Featured
How to build an LLM SEO readiness audit: a practical framework
nmsamuel
2
980
Conquering PDFs: document understanding beyond plain text
inesmontani
PRO
4
3.3k
My Coaching Mixtape
mlcsv
0
380
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
680
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.8k
Tell your own story through comics
letsgokoyo
1
1.1k
First, design no harm
axbom
PRO
2
1.4k
How to Think Like a Performance Engineer
csswizardry
28
2.9k
Design of three-dimensional binary manipulators for pick-and-place task avoiding obstacles (IECON2024)
konakalab
0
650
Un-Boring Meetings
codingconduct
0
490
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.6k
Getting science done with accelerated Python computing platforms
jacobtomlinson
2
570
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