What do these lines call? 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's going on here? I am assuming this is a special type of line separator. How it's called? Is this Ruby specific? What does it do squish

?

+3


source to share


2 answers


String#squish

comes from Rails and means that the final row of the row does not have multiple \n

ands \s

. From the docs :



Returns a string by first removing all spaces at both ends of the string, and then changing the remaining sequential groups of spaces to one space at a time.

+3


source


This is the syntax for Here Document ( HereDoc

), which is a multi-line 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

      

Gives you a string variable sql

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 spaces and new lines preserved.

In the HereDoc:



To call a method in a heredoc, place it after the opening id

A popular use of this quote is when you have a long line and you don't want to write it on one line and therefore use HereDoc for it, but you still don't want to keep the entire newline characters and spaces that are saved here and in In this case, you can simply 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

      

Gives you a string variable sql

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 resulting newlines and spaces in one space.

+2


source







All Articles