Brian Mehrman

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.

Postgresql Record Locking

ruby postgresql

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.

Optimistic vs Pessimistic Locking

ruby rails postgres

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.

Git Tips



Git is a free version control system designed for both small and large projects.

Class_eval vs Define_method

ruby meta programming

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.