Ruby implementation of algorithms