manipulating ruby ast
TRANSCRIPT
![Page 1: Manipulating Ruby AST](https://reader033.vdocuments.site/reader033/viewer/2022051123/5a64a14b7f8b9a57568b4e73/html5/thumbnails/1.jpg)
Manipulating Ruby AST@jonatasdp
![Page 2: Manipulating Ruby AST](https://reader033.vdocuments.site/reader033/viewer/2022051123/5a64a14b7f8b9a57568b4e73/html5/thumbnails/2.jpg)
ABSTRACT
SYNTAX
TREE
![Page 3: Manipulating Ruby AST](https://reader033.vdocuments.site/reader033/viewer/2022051123/5a64a14b7f8b9a57568b4e73/html5/thumbnails/3.jpg)
Why we need this?● Ruby is !● Adopt the Community Style Guide● Automatic checks● Autocorrect feature
![Page 4: Manipulating Ruby AST](https://reader033.vdocuments.site/reader033/viewer/2022051123/5a64a14b7f8b9a57568b4e73/html5/thumbnails/4.jpg)
Ruby is !
![Page 5: Manipulating Ruby AST](https://reader033.vdocuments.site/reader033/viewer/2022051123/5a64a14b7f8b9a57568b4e73/html5/thumbnails/5.jpg)
3 times !
![Page 6: Manipulating Ruby AST](https://reader033.vdocuments.site/reader033/viewer/2022051123/5a64a14b7f8b9a57568b4e73/html5/thumbnails/6.jpg)
Manipulate strings !
![Page 7: Manipulating Ruby AST](https://reader033.vdocuments.site/reader033/viewer/2022051123/5a64a14b7f8b9a57568b4e73/html5/thumbnails/7.jpg)
We have a few redundant syntaxes to explore!
![Page 9: Manipulating Ruby AST](https://reader033.vdocuments.site/reader033/viewer/2022051123/5a64a14b7f8b9a57568b4e73/html5/thumbnails/9.jpg)
(good || bad)? http://gorb.ideia.me
� if the code is bad if the code is good
![Page 10: Manipulating Ruby AST](https://reader033.vdocuments.site/reader033/viewer/2022051123/5a64a14b7f8b9a57568b4e73/html5/thumbnails/10.jpg)
Before Cop Anatomy...
Let’s play with the
AST
![Page 11: Manipulating Ruby AST](https://reader033.vdocuments.site/reader033/viewer/2022051123/5a64a14b7f8b9a57568b4e73/html5/thumbnails/11.jpg)
Using Ripper
$ ruby -r ripper -e 'p Ripper.tokenize("1 + 2")'
["1", " ", "+", " ", "2"]
![Page 12: Manipulating Ruby AST](https://reader033.vdocuments.site/reader033/viewer/2022051123/5a64a14b7f8b9a57568b4e73/html5/thumbnails/12.jpg)
Using Parser
$ ruby-parse -e '1+2'
(send
(int 1) :+
(int 2))
github.com/whitequark/parser
![Page 13: Manipulating Ruby AST](https://reader033.vdocuments.site/reader033/viewer/2022051123/5a64a14b7f8b9a57568b4e73/html5/thumbnails/13.jpg)
github.com/jonatas/rubocopularUsing Parser
![Page 14: Manipulating Ruby AST](https://reader033.vdocuments.site/reader033/viewer/2022051123/5a64a14b7f8b9a57568b4e73/html5/thumbnails/14.jpg)
RuboCop::Node
#type
#parent
#children
#sibling_index
#each_ancestor(*types)
![Page 15: Manipulating Ruby AST](https://reader033.vdocuments.site/reader033/viewer/2022051123/5a64a14b7f8b9a57568b4e73/html5/thumbnails/15.jpg)
Cop Anatomy
● #add_offense● #autocorrect(node)● #on_{type}● #investigate()
![Page 16: Manipulating Ruby AST](https://reader033.vdocuments.site/reader033/viewer/2022051123/5a64a14b7f8b9a57568b4e73/html5/thumbnails/16.jpg)
RuboCop::NodePattern
Let’s have some fun
with the
Node Pattern
![Page 17: Manipulating Ruby AST](https://reader033.vdocuments.site/reader033/viewer/2022051123/5a64a14b7f8b9a57568b4e73/html5/thumbnails/17.jpg)
github.com/jonatas/rubocopular
![Page 18: Manipulating Ruby AST](https://reader033.vdocuments.site/reader033/viewer/2022051123/5a64a14b7f8b9a57568b4e73/html5/thumbnails/18.jpg)
RuboCop::NodePattern
_ is something
… whatever
$ captures
{} for union
[] for intersection
![Page 19: Manipulating Ruby AST](https://reader033.vdocuments.site/reader033/viewer/2022051123/5a64a14b7f8b9a57568b4e73/html5/thumbnails/19.jpg)
NodePatternCompiler
![Page 20: Manipulating Ruby AST](https://reader033.vdocuments.site/reader033/viewer/2022051123/5a64a14b7f8b9a57568b4e73/html5/thumbnails/20.jpg)
RuboCop #autocorrect
![Page 21: Manipulating Ruby AST](https://reader033.vdocuments.site/reader033/viewer/2022051123/5a64a14b7f8b9a57568b4e73/html5/thumbnails/21.jpg)
FastCompiler
![Page 22: Manipulating Ruby AST](https://reader033.vdocuments.site/reader033/viewer/2022051123/5a64a14b7f8b9a57568b4e73/html5/thumbnails/22.jpg)
Replacing code with fast
![Page 23: Manipulating Ruby AST](https://reader033.vdocuments.site/reader033/viewer/2022051123/5a64a14b7f8b9a57568b4e73/html5/thumbnails/23.jpg)
Thanks
@jonatasdp
http://ideia.me