TiL: Double Splat Nil in Ruby

How Ruby can do what you don’t expect Ruby is a very clever language. It will take the code you’ve written and do it’s best to make sure it runs. Take for example the following method call: method_one('Hello', param_two: ' world') That looks simple enough as it takes two arguments, one positional and one keyword. Now, lets take a look at what the method is actually doing: def method_one(param_one, param_two) p param_one, param_two end Interesting. At first glance it might appear that this method would print Hello world, but there is a subtle difference between how the method has been defined and how it is called. Let’s see what it actually outputs: ...

June 9, 2025 · 2 min · 384 words · George Bell

TiL: Using the max_by method in Ruby

When trying to get an upper or lower value for a certain field in an object can be cumbersome. But when using the max_by method, this can actually be done quite elegantly. For example: persons = [ { "name" => "John McJohnson","age" => 34, "topScoreAtBowling" => 205}, { "name" => "Davey Jones","age" => 304, "topScoreAtBowling" => 300}, { "name" => "Willy Wonka","age" => 50, "topScoreAtBowling" => 200} ] If we had a bowling competition and wanted to see who had the topScoreAtBowling field, rather than looping through the array and looking at each object, we can simply use the max_by command as follows: ...

February 20, 2025 · 1 min · 170 words · Tomos Griffiths

Toggling Ruby scenarios in Drone using secrets

TiL how to toggle Ruby scenarios to be run using CI pipeline secrets Scenario In a relatively niche scenario, we have functionality that is being temporarily removed while new code is being developed. While we still want to keep our current scenarios for function X to use them again when it’s reintroduced, if they are left to run in our pipeline, every build will fail until it is reintroduced again. ...

November 7, 2024 · 4 min · 707 words · Leighton Taylor

TiL: The difference between exec and system in Ruby

Ruby provides a few different ways to execute commands against the underlying kernel programmatically, but they all work slightly differently. The simplest way to execute a command againts the shell is to surround it in backticks. For example, the following will run the command date and return whatever was output to $stdout. current_date = `date` However, there are also more explict methods you can call. In particular, there is exec and system which look like they do the same thing at a quick glance. However, they actually work very differently once you get down into what they are doing. ...

September 23, 2024 · 2 min · 425 words · George Bell

TiL: Doing multiple replacements with gsub in Ruby

gsub is great. It lets you replace characters in a string. All you have to do is give it a regex that will match whatever it is you want to replace and the string that you’d like to replace each match with. For example, if you wanted to replace the time of 18:00 with something a bit more human-readable, you could do the following: 'As always, tea time is at 18:00.'.gsub(/18:00/, '6pm') # => "As always, tea time is at 6pm." But what if you wanted to do something different for each match? Well, because gsub returns a string you can chain the methods together, which looks like the following: ...

July 30, 2024 · 2 min · 245 words · George Bell