Let’s start with a brief discussion about pattern matching in Ruby, what it does, and how it can help improve code readability.
If you are anything like me a few years ago, you might confuse it with pattern matching in Regex. Even a quick Google search of ‘pattern matching’ with no other context brings you content that’s pretty close to that definition.
Formally, pattern matching is the process of checking any data (be it a sequence of characters, a series of tokens, a tuple, or anything else) against other data.
In terms of programming, depending on the capabilities of the…
Setting up a proper monitoring overview over your application’s performance is a complex task. Normally, you’d first need to figure out what you need to monitor, then instrument your code, and finally make sense of all the data that has been emitted.
However, with a few things set in place, and an APM that natively supports Ruby, it’s easier than ever to take this step. In this post, we’ll show you how you can do it too.
To discover which pieces of your code are causing your performance issues, you’ll need to add instrumentation to it. …
This article has been modified from its original appearance in Playbook Thirty-nine — A Guide to Shipping Interactive Web Apps with Minimal Tooling, and tailored to fit this guest post for AppSignal.
There’s a lot of functionality that your app needs to handle, but that logic doesn’t necessarily belong in the controller or even the model. Some examples include checking out with a cart, registering for the site, or starting a subscription.
You could include all this logic in the controller, but you’ll keep repeating yourself, calling the same logic in all those places. You could put the logic in…
In today’s post, we will look into Ruby’s
#clone. We'll start with a real-life example that triggered this interest. After that, we'll dive deeper with the goal of learning how
#dup is implemented in Ruby and how it compares to
#clone. We'll then close of by implementing our own
#dup method. Let's go!
When I worked at a company that specialized in setting up campaigns for NGO’s to collect donations, I regularly had to copy campaigns and create new ones. For example, after the 2018 campaign ended, a new one for 2019 was needed.
A campaign usually had…
Happy new year, and welcome back to Ruby Magic! In this winter episode, we’ll dive into bindings and scopes. So put on your skis and follow us deep into the woods.
Last time, we looked at closures in Ruby by comparing blocks, procs and lambdas. Aside from the differences between the three types, we touched on what defines a closure.
A closure is a first-class function with an environment. The environment is a mapping to the variables that existed when the closure was created. The closure retains its access to these variables, even if they’re defined in another scope.
Performance monitoring is an important part of running a successful application. One of the most basic ways to tell the performance of something is to measure the duration each time it happens and distill statistics from it.
The mean or average of a collection of values is a good start to see how good or bad something behaves. It is calculated by summing all the values under consideration and then dividing by the number of occurrences.
In Ruby, this is what calculating the mean response time would look like:
(array.sum.to_f / array.length).round(2)
end durations = [1,2,3,4,5,6,7,8,9,0]
Ruby has various ways of performing iteration — loops, blocks and enumerators. Most Ruby programmers are at least familiar with loops and blocks but
Fiber often stay in the dark. In this edition of Ruby Magic, guest author Julik shines a light on
Fiber to explain flow controlling enumerables and turning blocks inside out.
We’ve discussed Enumerator in a previous edition of Ruby Magic, where we described how to return an
Enumerator from your own
#each method and what it can be used for. An even broader use case for
Fiber is that they…
In this edition of Elixir Alchemy, we’ll dive into Elixir’s GenStage module. Along the way, we’ll explain backpressure and we’ll write a Genstage to query the blockchain. Let’s start by discussing how using a GenStage can solve buffering problems.
Imagine you’re consuming data from an external source. That source could be anything “streamable” — such as reading a file line-by-line, a table in a database, or even a sequence of requests to a 3rd party API.
In such scenarios, where you need to stream data into your system, and probably do some processing on each data point, it’s common to…
Threads and asynchronous environments are initially a bit tricky. Without a good mental model to organize interaction, it is easy to get into trouble and end up with unexpected results. On top of that, testing asynchronous code can be difficult without the right tools or test patterns.
Thinking about threads as people and shared objects as ‘things’ that can be owned helps to organize the working of a multithreaded system. In this episode we will go through an example to learn all about testing asynchronous Ruby code.
If you are using Rails or Rack or really any application that as…
In this edition of Ruby Magic, we’ll show you how to use code written in C from Ruby. This can be used to optimize performance sensitive parts of your code or to create an interface between a C library and Ruby. This is done by creating extensions that wrap libraries written in C.
There are a lot of mature and performant libraries written in C. Instead of reinventing the wheel by porting them we can also leverage these libraries from Ruby. In this way, we get to code in our favorite language, while using C libraries in areas where Ruby…