Tips and methods to write a better Ruby code

Tips and methods to write a better Ruby code
Average rating: 5
(1 votes)

Thanks! You’ve rated this material!

‘Ruby is simple in appearance, but is very complex inside, just like our human body’. This quote by Yukihiro Matsumoto, the chief designer of Ruby, means a lot for Rubyists. 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, because if you know how it works – you’re able not only using it but are empowered to improve it! Here we collected for you some tips and methods to write a better Ruby code. Read and use!

Ones we already shared with you 5 Ruby methods 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).

Thanks to Toptal EngineersHannah Squier and Takehiro Mouri for these tips!

Interesting methods to write a better Ruby code

Thanks to Jesus Castello for this scope!

You can write Ruby methods yourself too. But when doing this, remember about the composed method technique that has 3 simple rules:

  1. Each method should do a single thing;
  2. Each method needs to operate at a single conceptual level;
  3. Each method needs to have a name that reflects its purpose.

Additional reading

  1. Learn about array methods and Ruby syntax with code snippets.
  2. Learn more about Methods in Ruby.
  3. Read the consideration of Ruby vs Elixir – when to use
  4. Interesting facts about Ruby
  5. Ruby books and tutorials
  6. Memoization in Ruby
  7. Effective Ruby: 48 Specific Ways to Write Better Ruby’ book from ‘Effective Software Development Series’

If you liked this article – subscribe to our weekly newsletter and Syndicode’s Telegram channel!

Rate this article, if you like it

Thanks! You’ve rated this material!

Got a project? Let's discuss it!

Kyiv Sofiivska 1/2a, 01001, Kyiv, Ukraine
Dnipro Hlinky 2, of. 1003, 49000, Dnipro, Ukraine
Kharkiv Otakara Yarosha 22, 61000, Kharkiv, Ukraine
Email info@syndicode.com