Ruby Code Blocks

Code Blocks Ruby code blocks are from the closure family Ruby blocks are found throughout ruby. They are a powerful feature that allow you to pass snippets of code to enumerable methods (e.g. each, select, detect) as well as custom methods useing the yield keyword. Blocks are nothing new, they use the computer science concept called closures. This concept was invented by Peter J. Landin in 1964. Closures were adopted by a version of a Lisp called Scheme in 1975. ...

January 13, 2019 · 4 min · Brian Mehrman

Postgresql Record Locking

Database Lock Modes Postgresql provides different locking modes to control concurrency within the database. Locks are automatically acquired by most Postgres commands to make sure tables are not dropped or modified while a command is executed. Locks can also be acquired manually for application level control. ActiveRecord provides the interface between the Rails application and the database. Using ActiveRecord we can create these database level locks. Setup For the examples in this post we will be using the following data setup with a new Rails application. ...

June 23, 2018 · 7 min · Brian Mehrman

Optimistic vs Pessimistic Locking

Data integrity is at risk once two sessions begin to work on the same records… Martin Fowler Optimistic Locking Is a data access strategy where it is assumed that the chance of conflict between sessions is very low. Changes from one session are validated before being committed to the database. Pessimistic Locking This an opposing data access strategy assumes that conflicts between sessions is highly likely. Conflicts are prevented by forcing all transactions to obtain a lock on the data (record or table) before it can start to use it. ...

May 29, 2018 · 4 min · Brian Mehrman

Git Tips

Git Git is a free version control system designed for both small and large projects. Tips Rebase Whenever I am working on a project with other developers I find that I need to rebase my feature branch with the common branch the rest of the team is using. This branch is usually called dev or ci. $ git fetch To make sure I have the latest branches I run a fetch to update all my local branches. ...

February 6, 2018 · 2 min · Brian Mehrman

class_eval vs define_method

Meta Programming Having to write redundant methods can lead to hundreds of lines of code that can be hard to maintain. Meta programming provides us with tools that allow us to write code that writes its self. Class Eval class_eval opens the class and adds the method to the class itself. While this takes longer to define a method it puts the new method in the ancestor chain where it can be accessed quicker than if you used define_method. ...

April 26, 2017 · 5 min · Brian Mehrman