Double Left Angle Bracket
There are a couple essential contexts in which you'll see <<
show up in Ruby. Head over to the Shift Operators page to see how else it is used.
Shovel Operator
Perhaps more commonly is as the Shovel Operator, which is conceptually used to "shovel" a value into a container. The more common term for this kind of action is append.
In the simplest case, the shovel operator is used to append a value to an array.
> items = [:a, :b]
=> [:a, :b]
> items << :c
=> [:a, :b, :c]
> items << [1,2,3]
=> [:a, :b, :c, [1, 2, 3]]
> items << true
=> [:a, :b, :c, [1, 2, 3], true]
heredoc
A heredoc (here document) is a way to create a multi-line string in Ruby. It's typically used for creating multi-line messages, SQL queries, or HTML.
There are several ways to define a heredoc, the most common being <<HEREDOC
, <<-HEREDOC
, and <<~HEREDOC
.
A basic example of a heredoc is this:
query = <<SQL
select *
from users
where active = true
and role = 'admin'
SQL
execute(query)
If you're curious what the different forms do, head over to the heredoc page.