Luhn algorithm in Elixir: implementation, refactoring, and benchmarking

Some time ago, I have encountered a programming exercise in which the goal was to implement Luhn algorithm for credit card number validation. At first I implemented the algorithm in Ruby and then decided to implement it in Elixir. Eventually, I like how Elixir version looks like. In this article Elixir 1.2.2 is being used. Initial version It is assumed that credit card numbers are being read from a file and that check digit is included in a credit card number. The Luhn algorithm is quite straightforward. Description from Wikipedia 1: The formula verifies a number against its included check digit, which is usually appended to a partial account number to generate the full account number.

A challenge: remove tags from a string.

Recently I have implemented a small piece of functionality, which is suitable to be a small challenge. Description Given a string with opening and closing tags (e.g., <mark></mark>), return a string without tags and indices of opening and closing tags. For example, given a string: "we eat <mark>healthy</mark> and <mark>tasty</mark> food." We expect to receive a string: "we eat healthy and tasty food.", and an array with pairs of indices: [[7, 14], [19, 24]]. There are two pairs of indices, because there were two pairs of <mark></mark> tags. Ruby First, I tried to solve this problem imperative way, which did not work very well.

How to set Cache-Control and Expires headers for Paperclip uploads.

In Ruby on Rails applications, gem Paperclip is often used with gem Fog, which has a feature of setting up Cache-Control and Expires headers for file uploads. Assuming, there is the following configuration in config/application.rb file: config.paperclip_defaults = { storage: :fog, fog_credentials: { provider: "Local", local_root: "#{Rails.root}/public" }, fog_directory: "", fog_host: "localhost" } Cache-Control and Expires headers can be set up by adding related attributes to the paperclip configuration: config.paperclip_defaults = { ...previous code... fog_file: { 'Cache-Control' => 'max-age=86400', 'Expires' => 1.week.from_now.httpdate } } Remember that max-age is represented in seconds (so 86400 is 1 day).

How to fix problems when migrating a Rails app from MySQL to PostgreSQL

Recently, at we have been working on migrating a Ruby on Rails application from MySQL to PostgreSQL. Depending on the complexity of an app, there might be many caveats during migration. Here are some notes on issues that we solved during the migration. This article assumes that we work with: Ruby on Rails 4; MySQL 5.6; PostgreSQL 9.4. Quoting and Boolean fields MySQL uses backticks (`) for quoting, while in PostgreSQL double quotes (") are being used. MySQL uses TINYINT datatype to represent boolean fields. Therefore, 1 would represent true and 0 would represent false. PostgreSQL has native boolean datatype.