I went to Rubykaigi 2014 last week, there were lots of great talks about ruby itself, rails tips and more. Here I’ll share 3 things I feel worth to share for rails developers.
Synvert = syntax + convert, makes it easy to convert ruby code automatically
Synvert is a gem made by the same author of bullet gem. Using it you can convert your rails 3
before_filter to rails 4 syntax
should have to
expect(…).to have or ruby 1.8 hash to 1.9 hash syntax with just one command, and it does support more. It’s well tested so no worry for human error.
You could define your own rules to convert code, or use those built-in very common used snippets for rails, rspec and factory_girl.
Installation and usage
1 2 3 4 5 6
git diff I got
How does it work
AST(Abstract Syntax Tree) is used internally, it parses your source code to meaningful structures. It’s like grammar in English, one phrase can be broken down to words and some are noun, some are verb, it’s like saying “let’s replace all the verb ‘walk’ to ‘run’”. I highly recommend you to check out the slide if you want to learn more.
For instance, it breaks a method call to receiver, message and arguments. A typical method call is a type of “send node”, class definition is “class node” and there is “block node”.
Going the distance
To be honest I’m not sure I fully understand the algorithm of calculating distance between 2 words, but the great part is @ schneems used it to improve rails generator command to suggest possible commands when you had a typo.
Please see the pull request for details. Emit suggested generator names when not found #15497
If you ever make a cli command, you could follow the same pattern, using the algorithm to suggest candidates instead of just showing plain error message.
Good to see how how he adapted scientific algorithm to solve real world problem.
Speeding up Rails 4.2
Interesting to see how @tenderlove find room for optimization and how to measure it.
- benchmark-ips, benchmarks a blocks iterations/second. For short snippits of code, ips automatically figures out how many times to run the code to get interesting data. No more guessing at random iteration counts!
- allocation_tracer, allows to trace object allocation.