Skip to content

Instantly share code, notes, and snippets.

@mehdi-farsi
Last active April 23, 2018 12:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mehdi-farsi/2173db64fcad52e347e6a5972b9968e3 to your computer and use it in GitHub Desktop.
Save mehdi-farsi/2173db64fcad52e347e6a5972b9968e3 to your computer and use it in GitHub Desktop.
HEREDOC: Method chaining

In this example, we remove the trailing spaces and \n from our SQL query

sql = <<-SQL.gsub("\n", ' ').squish
SELECT MIN(u.popularity)
FROM users u
WHERE
  u.created_at <= #{Time.now} AND u.created_at >= #{Time.now - 7.days}
SQL

p sql

Produces

"SELECT MIN(u.popularity) FROM users u WHERE u.created_at <= 2018-04-23 21:42:00 +0200 AND u.created_at >= 2018-04-16 21:42:00 +0200"

NB: String#squish is a method provided by Ruby on Rails

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment