How *not* to use heredoc in Ruby
Sometimes you inherit a codebase that’s so bad, it confuses your IDE. Let’s start with this beauty:
User.
joins(<<-SQL).
LEFT JOIN industries
ON industries.id = users.industry_id
SQL
where.
not(industries: { id: 1 })
What’s your first thought when you see something like that? If you’re convinced it’s a broken code then feel free to play with it your Rails project, just substituting model / table names for something more appropriate in your case.
The above “query” works due to the same reason as the code below:
p <<SQL.gsub(/\s+/, " ").strip
SELECT * FROM users
ORDER BY users.id DESC
SQL
After I got my head around this unintuitive syntax, as an exercise I came with a following brainfart, which you may treat as a dessert:
bullet_time = <<""::b
this works as well
But FFS, don’t do this
p bullet_time
Tweet