#5931·jekyll

Issues with lazy initializations using the ||= operator when iterating document collections

Author: sverrirsCreated Mar 3, 2017Updated May 12, 2026
Labelshas-pull-requestpinnedbug :bug:

While developing a pagination plugin I came a cross a strange error when users tried to build jekyll using the --drafts argument

jekyll build --drafts

I traced the problem to the use of the ||= operator in the document.rb class. One example is the date function. Using lazy assignment for member variables actually causes issues when iterating over documents that do not have these fields set as the collection iterator is broken as soon as the values are assigned.

This surfaces when using the --drafts switch because draft documents may or may not have the date: front matter set.

I encountered this problem when I was simply doing an Array.sort on a list of posts and the problem occurred even before any of my code was called. Namely in the <=> compare function in the Document class (this function accesses lazy initialized values).

lib/jekyll/document.rb:61:in `date': can't add a new key into hash during iteration (RuntimeError)

The use of this ||= operator should perhaps be revised.