# Ruby Madness - Right Hand If Statements and Syntax Error, Unexpected end-of-input, Expecting End

DevFeed: [Ruby Madness - Right Hand If Statements and Syntax Error, Unexpected end-of-input, Expecting End](<https://devfeed.tech/articles/ruby-madness-right-hand-if-statements-and-syntax-error-unexpected-end-of-input-expecting-end-28302.md>)

Original publisher: [Read original article](<http://fuzzyblog.io/blog/ruby/2019/11/07/ruby-madness-right-hand-if-statements-and-syntax-error-unexpected-end-of-input-expecting-end.html>)

Author: Fuzzygroup

Published: 2019-11-07T00:00:00Z

Content type: tutorial

Language: en

Sources: [Scott Johnson](<https://devfeed.tech/sources/scott-johnson.md>)

Topics: [Ruby](<https://devfeed.tech/topics/ruby.md>), [Code](<https://devfeed.tech/topics/code.md>), [Parser](<https://devfeed.tech/topics/parser.md>)

Tags: [code](<https://devfeed.tech/tags/code.md>), [debugging](<https://devfeed.tech/tags/debugging.md>), [issue](<https://devfeed.tech/tags/issue.md>), [lint](<https://devfeed.tech/tags/lint.md>), [ruby](<https://devfeed.tech/tags/ruby.md>), [syntax](<https://devfeed.tech/tags/syntax.md>)

## AI overview

A Ruby syntax error reported at the end of a class can result from an earlier right-hand if statement that appends nothing to an array. The article shows how to use ruby -c and progressively reduce the file to locate the subtle parsing problem, then presents the corrected method.

## Source excerpt

The Eagle's Nest AirBnB Cabin on the Ohio River; One of my favorite places... Last night I was on a coding tear, that moment when the solution is just crystal clear. That situation where something you've struggled with for days / weeks / months and then it all comest together. The result of this was a whole bunch of code stuck in a single model and then this morning when I was ready to run it across the entire dataset, I got this crap: ruby -c app/models/habit.rb app/models/habit.rb:592: syntax error, unexpected end-of-input, expecting end I censored hate this error. Line 592 is the end of the class and that means that the ruby parser just blithely skipped to the end and really has no useful positional information for you. Note: If you're not familiar with ruby -c, it is a syntax checker that makes sure your file is parseable. Think of it as a poor man's lint. My usual approach to finding this error is to cut our half the code in the file and then re-run the ruby -c process. And then repeat that until you get this: ruby -c app/models/habit.rb Syntax OK And that's how I ended up finding the error but the error was ridiculously subtle. Here's the error: def self.with_feeds habits_with_feeds = [] Habit.active.each do |habit| habits_with_feeds << if habit.has_feed? end habits_with_feeds end And here's the corrected version of the method: def self.with_feeds habits_with_feeds = [] Habit.active.each do |habit| habits_with_feeds << habit if habit.has_feed? end habits_with_feeds end Now, this is subtle, so I'm going to just call it out. The issue was this line: habits_with_feeds << habit if habit.has_feed? versus habits_with_feeds << if habit.has_feed? Clearly, to the ruby parser, the lack of something being added to the array qualifies as a missing end. Yeah I get that but damn. I'm personally a huge fan of right hand if statements due to the reduction of 3 lines down to 1 but this is a case where they bit me hard. Sigh.