Ylan Segal

The REPL: Issue 15 - October 2015

Speed up with Materialized Views on PostgreSQL and Rails

Materialized views are a way to cache the result of expensive database computations, right on the database. Used in the right manner, they can make speed up performance significantly. As with any other caching mechanism, there exists some caveats about invalidating the cache when underlying data changes. This guide shows how to leverage this database feature in a Rails app. Clear and to the point.

Debugging a Memory Leak on Heroku

Richard Schneeman writes another insightful post on how to make Ruby applications better. In this case, he talks about how to identify memory leaks (as opposed to memory bloat) and different techniques to mitigate memory leaks. Plenty of good techniques discussed.

What Would Feynman Do?

One of my favorite books, is Surely You’re Joking, Mr. Feynman!, by Nobel-prize winning physicist Richard Feynman, a collection of anecdotes from his life, in which is unique way of viewing the world and whimsical approach to problem solving is highlighted. This blog post imagines Mr. Feynman at a job interview, where he is asked to solve a “later-thinking” puzzle. It’s hilarious. If you enjoyed it, don’t hesitate to read the book.

Communication: how to be a better software developer

Targeted to software developers trying to level up, this articles has great tips on how to be a better communicator and why it’s important. The advice resonates with me. Really, every one can benefit from being better at people, no?

Talk: LA RubyConf 2015

Yesterday, I had the pleasure and privilege of presenting at LA RubyConf 2015. The conference was great. The presentation slides for my talk, Practical Unix for Ruby and Rails is now available.

It is powered by the awesome Remark.js, which has awesome features, like presenter mode that shows you notes next to each slide. Hit [P] to see my notes for the slides.

Update: Confreaks has posted the video of the talk

The REPL: Issue 14 - September 2015

How We Ended Up With Microservices

Phil Calçado writes a detailed post on the non-technical side of why Soundcloud moved away from a monolithic Rails app, in favor of a microservices architecture. Main reason: productivity. They were able to reduce their time-to-launch of new features from 66 days to 16 days.

A Gentle Introduction To Actor-based Concurrency

Originally published 2 years ago, Practicing Ruby provides a great explanation of what the Actor model looks like in Ruby. He solves the Dinning Philosophers Problem with bare ruby, the with Celluloid and then shows a simple implementation of actors in ruby would look like. Great read.

Implementing Worker Threads in Rails

Did you know that when a process is forked in ruby, only the main thread is copied and all other threads are dead? Neither did I, until I ran into it recently. Solving threading issues is very hard. This post has great techniques on how to use threads in Rails, even if using forking servers.

The REPL: Issue 13 - August 2015

Making Architecture Matter

In this keynote at OSCON 2015, Martin Fowler gives a great overview about how to think about software architecture to get the most benefit. As quoted in the comments:

It’s particularly relevant now as we push more and more toward continuous delivery, continuous deployment, features updated over the Internet all the time. That degree of being able to respond to change becomes important. That’s the economic reason why software architecture is important, because if we don’t keep good architecture, we are, in the end, deceiving our customers — in fact, stealing from our customers — because we’re slowing down their ability to compete.

Binary pattern matching in Elixir

Zohaib Rauf writes a great post that shows the great power of pattern matching in Elixir. Step by step, he builds a small module that parses PNG binaries. The explanations are clear and the resulting code is really elegant: One main function that parses the header and calls a private recursive function that parses each of the chunks. Worth a read, even if you are not into Elixir (yet!).

Work Hard, Live Well

Dustin Moskovitz writes about work life balance in the software industry and his personal experience at Facebook. The article resonates with me: I often say that in the modern world, sleep is a competitive advantage: Being tired decreases cognitive ability. When I was a freshman in college, I stayed up late to study for a physics exam, which I presented after only 4 hours of sleep. I had flunked because of basic algebraic mistakes applying the correct physical formulae. I took the lesson to heart. After that, for me exam preparation always included adequate rest.

Using ActiveRecord::Base#merge

The API for ActiveRecord::Base is very large, which makes it easy to miss some of the great convinences it affords.

class Account < ActiveRecord::Base

Account.new.methods.count # => 558
Object.new.methods.count # => 123

ActiveRecord::Relation#merge merges the conditions of two relations, allowing the use of scopes on both sides. This allows not repeating where clauses in separate places in the code base. Example, extracted from the Rails documentation:

# Instead of:

Post.joins(:comments).where(published: true, comments: { spam: false })

# => SELECT "posts".* FROM "posts" INNER JOIN "comments" ON "comments"."post_id" = "posts"."id" WHERE "posts"."published" = ? AND "comments"."spam" = ?  [["published", "t"], ["spam", "f"]]

# Lets take advantage of scopes in Post and Comment:

class Comment < ActiveRecord::Base
  scope :non_spam, -> { where(spam: false) }

class Post < ActiveRecord::Base
  scope :published, -> { where(published: true) }


# => SELECT "posts".* FROM "posts" INNER JOIN "comments" ON "comments"."post_id" = "posts"."id" WHERE "posts"."published" = ? AND "comments"."spam" = ?  [["published", "t"], ["spam", "f"]]

This way, each scope stays with the model it belongs in, removing any danger of duplicating domain logic (like what is consider spam) to a single place in the code. This technique is especially useful when taking advantage of more complicated (possibly parametized) scopes.