One ruby tip every week ~by @squadracer

Joined August 2023
Include

Only show posts containing:

Exclude

Hide posts containing:

Time range
-
Minimum likes
goruby Ruby not concise enough? In the source code, you’ll find a file called golf_prelude.rb. Include it in the compilation and you get goruby where each method has its own abbreviation, the String class behaves like an Array, … to be as concise as possible!
1
31
||= vs defined? Cache false/nil ||= is the most common way of caching. Except that it doesn’t handle false and nil! defined? can be a solution by checking whether the variable is defined. ⚠️ Be sure to take into account when the parser defines the variable!
1
36
A more interesting example highlighting the parser step. In the 1st if, the parser sees and creates the a variable after the condition, which will therefore be false at runtime. In the 2nd case, it sees and creates b before! (A if inline is therefore not equivalent!)
1
13
local variable, a detail on how they work They are created when the parser encounters them, not dynamically at runtime! Furthermore, a if does not create a new scope, making variables accessible after the scope has ended. This is also the case for unless, while, until and for.
1
29
The main benefit is to be able to alter the object in the method chain. Another use case is to be able to inspect an intermediate state without having to rewrite everything.
1
18
tap For method chaining The tap method, defined in Kernel, returns self which is passed as a block argument. To be distinguished from then, which returns the result of the block.
1
1
26
⚠️ There is a subtle difference with the splat operator. It doesn’t handle nil. Well, not yet, because after more than 10 years, it will in Ruby 3.4.0 (see Issue 20064: bugs.ruby-lang.org/issues/20…).
1
11
**{double splat operator} The double splat operator (or keyword splat) has been around since Ruby 2.0. It’s the equivalent of the splat operator but for Hash.
1
23
fast-ruby presents some interesting comparisons, like while vs loop, which do you think is faster? github.com/fastruby/fast-rub… Link to Ruby documentation for Benchmark: ruby-doc.org/3.3.5/stdlibs/b… squadracer.com/en/weeklytips…
16
Comparing implementations is generally more interesting, and can be done with bm or bmbm. The bmbm is run twice to « warm up » the code for a more accurate result. For example, compare flat_map and map { ... }.flatten and check that flat_map is indeed faster.
1
15
Need to speed up? Benchmark allows you to measure the execution time of a block with measure. You’ll find user and system CPU times, as well as the sum of the two. In brackets, the actual time. The unit is seconds. In this example, it’s long but not CPU-intensive.
1
30
The else is only allowed if there are rescue and can therefore be used wherever rescue can be used: block, method and even class!
1
12
What’s the point, given that the code could have been placed in the begin? Well, the code in the else is no longer protected by the rescue. This means you don’t have to deal with an unexpected exception as a side-effect.
1
3
rescue ... else A case to handle if there are no exceptions? Ruby allows default case handling only if there is no exception with else!
1
15