Tips and methods to write a better Ruby code
Iren Korkishko Jul 4
Originally I shared this material to Syndicode blog.
'Ruby is simple in appearance, but is very complex inside, just like our human body'. This quote by Yukihiro Matsumoto means a lot for Rubyists. Because Ruby is intuitive and beautiful but has many work secrets that you don't realize. And knowing them will obviously make you a better developer. If you know how it works - you're able not only using it but are empowered to improve it! Here I collected some tips and methods to write a better Ruby code.
Before I have already collected 5 basic Ruby methods that you should be using to write a better code. But the learning is a loop, so repeating and improvement will definitely help you to empower your development skills.
To make sure, that your code is legible, modular, and concise let's start with
Ruby best practices:
- 'case/when' and 'unless/until/while' conditionals, instead of lengthy 'if' statements, allow you to check whether a variable is set to many different values and is particularly useful when responding to user input.
- to group together a collection of inputs into an array use the splat (*) operator for methods with a variable number of inputs.
- using 'monkey patch' pre-defined methods you can easily rewrite predefined methods so they work better for our specific needs.
- avoid for loops and use the 'each' method with a block.
- use symbols instead of strings in hashes.
- the double bang (!!) is an elegant way of creating the common 'does some variable exist?' (to determine if a value exists) method.
- don't write comments for a wrong reason ('Good code is like a good joke: it needs no explanation').
- if possible, make your code shorter using ternary operators.
- avoid using 'before_action' to set variables: use a memoized finder instead.
- use Symbol garbage collector (GC).
Interesting methods to write a better Ruby code
- Integer#digits method
- The Tap method
- Array#values_at method
- Hash#transform_values method
- Kernel#itself method (for Ruby 2.2 that will no longer be supported)
- Array#count method
- Enumerable#cycle method
Thanks to Jesus Castello for this scope! I really enjoed the examples! (Check them as well by following the links).
You can write Ruby methods yourself too. But when doing this, remember about the composed method technique that has 3 simple rules:
- Each method should do a single thing;
- Each method needs to operate at a single conceptual level;
- Each method needs to have a name that reflects its purpose.
- Learn about array methods and Ruby syntax with code snippets.
- Learn more about Methods in Ruby.
- Read the consideration of Ruby vs Elixir - when to use
- Interesting facts about Ruby
- Ruby books and tutorials
- Memoization in Ruby
- 'Effective Ruby: 48 Specific Ways to Write Better Ruby' book from 'Effective Software Development Series'
Thanks for reading!
p.s. What other tips, methods, and techniques you can advise to make a better Ruby code?