Syntax Errors :: 0x02

Leaping straight beyond beginner's tutorials to intermediate coding exercises, you'll find code from Jim Weirich's version of the Gilded Rose Kata listed below.

Your task is not to solve that coding exercise, but instead to figure out:

  1. The one character that was accidentally deleted when copy-pasting the update_quality method into an editor which will crash the program unless fixed.

  2. The reason why the error logs don't seem to call out the true source of the syntax errors, and instead report on issues many lines away from where the problem was introduced.


WARNING: You're about to see some very brittle code.

But you won't need to understand how this code sample works to complete this exercise.

Instead, try to spot (1) and (2) visually, without running the code.


def update_quality(items)
  items.each do |item|
    if item.name != 'Aged Brie' &&
       item.name != 'Backstage passes to a TAFKAL80ETC concert'
      if item.quality > 0
        if item.name != 'Sulfuras, Hand of Ragnaros'
          item.quality -= 1
        end
      end
    else
      if item.quality < 50
        item.quality += 1
        if item.name == 'Backstage passes to a TAFKAL80ETC concert
          if item.sell_in < 11
            if item.quality < 50
              item.quality += 1
            end
          end
          if item.sell_in < 6
            if item.quality < 50
              item.quality += 1
            end
          end
        end
      end
    end
    if item.name != 'Sulfuras, Hand of Ragnaros'
      item.sell_in -= 1
    end
    if item.sell_in < 0
      if item.name != "Aged Brie"
        if item.name != 'Backstage passes to a TAFKAL80ETC concert'
          if item.quality > 0
            if item.name != 'Sulfuras, Hand of Ragnaros'
              item.quality -= 1
            end
          end
        else
          item.quality = item.quality - item.quality
        end
      else
        if item.quality < 50
          item.quality += 1
        end
      end
    end
  end
end

Think you've figured it out? Confirm by revealing the answer block below.

# [ANSWERS] #

 # Take note of the colorization for the following code sample.

  if item.name == 'Backstage passes to a TAFKAL80ETC concert
    if item.sell_in < 11
      if item.quality < 50
        item.quality += 1
       end
     end
     if item.sell_in < 6
        if item.quality < 50
          item.quality += 1
        end
      end
    end
  end
end
if item.name != 'Sulfuras


 # What you'll find is that due to the missing quote on the string in the first
 # line of this snippet, the entire chunk of code that follows it is parsed
 # as if it were part of the string and *not* as code.

 # And because it interprets the single quote before Sulfaras as the end of a
 # a string rather than the beginning, the word Sulfaras itself is parsed
 # as a constant.
 #
 # Then in addition to that issue being detected, Ruby 3.4 will also inform you
 # that the condition on the first line listed above never had a matching `end`
 # keyword. This happens because what it actually "sees" is one big string
 # of raw text and not code.
 #
 # So even though Ruby's error messages are helpful here if you can reason about
 # how the code is being parsed, the easier and more immediate way to spot this
 # particular type of error is just to notice that the colorization is off,
 # and then look at where that starts happening and try to see if there's an
 # obvious issue right around there.

Related Notes :: 0xF002