Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are these strings called? What is squish? Ruby

I found this code:

sql = <<-SQL.squish
        UPDATE #{klass.constantize.table_name}
           SET uuid = uuid_generate_v4(), updated_at = now()
         WHERE id IN (#{group.map(&:id).join(',')})
           AND uuid IS NULL
      SQL

What is going on here? I assume this is some special kind of string delimiter. What is it called? Is it Ruby specific? What is squish doing?

like image 481
Jwan622 Avatar asked Mar 21 '17 17:03

Jwan622


People also ask

What is squish in Ruby?

Returns the string, first removing all whitespace on both ends of the string, and then changing remaining consecutive whitespace groups into one space each. Note that it handles both ASCII and Unicode whitespace. %{ Multi-line string }. squish # => "Multi-line string" " foo bar \n \t boo". squish # => "foo bar boo"

What is a Heredoc Ruby?

What is heredoc? Heredoc allows us to specify a string as a block of texts where new lines and indentations are maintained. This is useful in Ruby for various cases like specifying multiline text strings, multiline method dynamic definitions and more.


2 Answers

String#squish comes from Rails and means that resulting inline string is without multiple \n and \s. From docs:

Returns the string, first removing all whitespace on both ends of the string, and then changing remaining consecutive whitespace groups into one space each.

like image 78
Ilya Avatar answered Sep 22 '22 15:09

Ilya


This is a syntax for a Here Document (HereDoc), which is a multiline string in Ruby.

For example, this:

sql = <<-SQL
        UPDATE #{klass.constantize.table_name}
           SET uuid = uuid_generate_v4(), updated_at = now()
         WHERE id IN (#{group.map(&:id).join(',')})
           AND uuid IS NULL
      SQL

Will give you an sql string variable whose value is literally:

         UPDATE #{klass.constantize.table_name}
           SET uuid = uuid_generate_v4(), updated_at = now()
         WHERE id IN (#{group.map(&:id).join(',')})
           AND uuid IS NULL

with all the spaces and newlines preserved.

From the HereDoc docs:

To call a method on a heredoc place it after the opening identifier

A popular usage of that quote is when you have a long string and you don't want to write it on a single line, and hence use a HereDoc for it, but you still don't want to keep all the newline characters and white spaces that the HereDoc preserves, in which case you can just call squish (which is a method added by Rails) to remove them. For example, this:

sql = <<-SQL.squish
        UPDATE #{klass.constantize.table_name}
           SET uuid = uuid_generate_v4(), updated_at = now()
         WHERE id IN (#{group.map(&:id).join(',')})
           AND uuid IS NULL
      SQL

Will give you an sql string variable whose value is literally:

UPDATE #{klass.constantize.table_name} SET uuid = uuid_generate_v4(), updated_at = now() WHERE id IN (#{group.map(&:id).join(',')}) AND uuid IS NULL

With all consequent newline characters and spaces squished into one whitespace.

like image 32
Tamer Shlash Avatar answered Sep 22 '22 15:09

Tamer Shlash