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 a model, but sometimes, you need access to things that are easily available in the controller, like an IP address, or a parameter in a URL. …
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 loads of configuration options, which I didn’t really feel like setting up again. It would take quite some time and was error-prone. So I started by copying the DB record and went from there. …
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.
We’ve explored Ruby’s equivalent to first-class functions, but we conveniently skipped over environments. In this episode, we’ll look at how that environment works for closures, classes and class instances by examining how Ruby handles lexical scope through its bindings. …
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]
mean(durations) #=> 4.5 …
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 can “suspend a block” mid-flight. …
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 use a buffer to read in a few items, process the whole batch, and then fetch a new set into the buffer. …
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 a web browser front end you are in an asynchronous environment. The Rack
#call is always called asynchronously. So whether you know it or not, there is a good chance you are already using multithreaded components. …
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 isn’t traditionally strong. …
Back when I was just starting in Ruby, there was no Bundler and gems had to be installed the hard way. In Rails, this meant running
rake gems:install a million times, fixing occurring bugs along the way, until the command passed with no errors. Today, we’re going to create a gem the old school way, after looking into what gems are and how they work.
RubyGems are an easy way to extend your own code with functionality written by other people. For example, instead of writing your own authentication/authorization code, you can use Devise, or if you want to re-size uploaded images you can use CarrierWave. …
Being one of Erlang’s most amazing features, hot code reloading is sometimes compared to replacing the wheels on a driving car and it’s majestically demonstrated with phone calls and hot bug fixes in Erlang: The Movie. But, how does it work under the hood?
In this edition of Elixir Alchemy, we’ll dive into hot code reloading to see how Erlang’s code server handles seamless code upgrades in Elixir. To understand how all this works, we’ll start at the module level and work our way up. Let’s get started!
The first part of the magic of hot code reloading is Erlang’s code server’s ability to run multiple versions of a module simultaneously. It allows existing processes to run to completion without having to be restarted or having their running code changed. …