Category: Ruby
-
What is a Ruby block?
Ruby blocks are fairly easy to pickup, use, and make work if you just follow the common examples. But at some point you might realize that you don’t really understand what is going on in the same way as you might with variable assignment or a method call. Let’s start with a basic definition of…
-
The differences between Ruby blocks, procs, lambdas and closures.
Grappling with the differences between Ruby blocks, procs, lambdas, and closures is confusing at first. To fresh eyes they looks kinda weird and complicated compared to other parts of Ruby which are more familiar. But once you get them down you realize how cool and useful they really are. We are here to walk you…
-
Should I use a Struct or an OpenStruct (Ruby)?
At first glace the choice between using a `Struct` or an `OpenStruct` is just preference. They are very similar and (mostly) behave the same way. Pretty similar, right? Actually there are some differences and these should inform your choice. The choice revolves around the specific characteristics of your data structure so lets look at those.…
-
How to get the min and max from an Array (Ruby)
Arrays have a couple methods to get the minimum and maximum. Three common ones are .min, .max, .minmax. .max will get you the maximum value. .min will get the minimum value. If you need both the minimum and the maximum from an array then the best solution is using the ruby method minmax. It will…
-
Binstubs
Binstubs, short for “binary stubs,” are small executable scripts that act as wrappers around Ruby executables or scripts. They are commonly used in Ruby projects managed with Bundler to provide a convenient way to execute commands without needing to prepend them with bundle exec. When you run `bundle install –binstubs` Bundler will create a bin…
-
Ruby Benchmark
If you just want to see how fast a bit of code is you can use the Benchmark module which is part of the Ruby Standard Library. If you want to compare two or more bits of Ruby code that do the same thing and see how fast they are against each other then you…
-
Ruby Case statements from if/else
Reading nested if statements making you sad? Case statements can be the answer. Refactoring your if statements can clear up complicated code and make spotting bugs much easier. Nested if statements often lead to complex and hard-to-read code. They can grow over time as new conditions are added, making it difficult to maintain and debug.…
-
Classes, Hashes, Structs and OpenStructs in Ruby
Intro If I am looking to maximize the performance of a Ruby app what should I be using? Hashes, Structs, OpenStruct, or Classes? New Ruby programmers love hashes. I love hashes. They are very flexible and tempting to add in everywhere. Even when refactoring a slow part of the system in Kafka microservices so that…
-
Ruby Native Extensions
In Ruby, native extensions (also known as C extensions or native gems) allow you to write parts of your code in the C programming language, which can be linked and executed as part of a Ruby program. Native extensions are typically used for performance-critical tasks, interacting with low-level system libraries, or providing access to functionality…
-
Cleaning up a messy Gemfile
Maintaining a clean and organized Gemfile is crucial for the long-term health and readability of your Rails application. As applications age, their Gemfiles can accumulate complexities and messiness. In this guide, we’ll explore some rules and best practices for crafting a clean, readable Gemfile that fosters maintainability. 1. Group Gems Effectively Divide your gems into…