How to find consecutive characters with Regex

This works by first matching against any alphanumeric character (\w) and storing this in a capture group (think a temporary variable). We then use (\1+) to tell Regex to match only those characters that have the same exact character occurring immediately after it at least once. The \1 refers to our previously matched capture group […]

Aug 16, 2020

Building a Sinatra app in 5 minutes

Sinatra is a micro web framework for Ruby. It provides us with the bare basics to quickly spin up a small back-end. It is comparable to Python’s Flask or Node.js’s Express.

Aug 10, 2020

How to login a user in Capybara tests when using Devise

Assuming we are using RSpec & Capybara to write our feature tests and utilizing Devise for authentication, we can login users in our tests like so: Include Warden test helpers Create a spec/support/devise.rb file with the following configuration: Include this support file in spec/rails_helper.rb: Use the login_as helper method Now we can use the login_as […]

Aug 09, 2020

Rendering different layouts in Rails depending on whether the user is logged in

Assuming we have two different layout files: application.html.erb – for logged in users guest.html.erb – for guest users We can add the following bit of code in our base ApplicationController: Assuming all our custom controllers extend ApplicationController as is the default convention, then our views will use the application layout when the user is signed […]

Aug 08, 2020

How to check for palindromes in Ruby

A palindrome is a sequence of characters that reads the same backwards or forwards (e.g. anna, bob, madam). First solution We begin by creating a reversed string which consists of the following steps: Split the string into array of characters Reverse the array of characters Join the reversed array so that we get a whole […]

Aug 01, 2020

How to create a sticky footer with Flexbox

Creating a footer that sticks to the bottom of the browser window is something that most web developers have to tackle at some point in their careers. The challenge of the sticky footer is ensuring that the footer appears right at the bottom of the screen even if there is very little content on the […]

Jul 25, 2020

How to check how many times a character occurs in a string in JavaScript

When solving katas on Codewars, I often find myself needing to do some variation of find out how many times a character occurs in a given string and then doing some further steps with the result. Here’s my preferred solution to this task: We make use of the RegExp constructor to create a dynamic regular […]