What does “<<-” mean in Ruby?

前端 未结 4 1292
日久生厌
日久生厌 2020-12-01 15:41

For example:

code = <<-EOH
    bundle install
    bundle exec unicorn -c /etc/unicorn.cfg -D
EOH

What does this code do? What is

相关标签:
4条回答
  • 2020-12-01 16:09

    It's called heredoc. An easy way to define multiline strings which may include single or double quotes without needing to escape them.

    See more here, for example.

    Often you use heredocs to define large chunks of code. Some editors know about this and can highlight syntax for you there (if you specify language). Look:

    strings vs heredocs

    0 讨论(0)
  • 2020-12-01 16:24

    You can pass multiple commands in one block like this in Chef Recipe

       bash 'Install ftppwd' do
        code <<-EOH
       mkdir #{ftppwd_dir}
       chmod 775 #{ftppwd_dir}
       chgrp #{batch_id} #{ftppwd_dir}/*
       chown #{batch_id} #{ftppwd_dir}/*
       EOH
    
    0 讨论(0)
  • 2020-12-01 16:26

    Looks to me like heredoc. The - allows the ending delimiter to ignore whitespace before it.

    A simple Google Search gave me this.

    0 讨论(0)
  • 2020-12-01 16:33

    There is also a newer HEREDOC syntax for Ruby <<~END that more closely resembles what you would typically see in most shells and other languages with the ~ instead of the - to tell Ruby to strip the leading whitespace to match the least indented line in the block.

    https://infinum.co/the-capsized-eight/multiline-strings-ruby-2-3-0-the-squiggly-heredoc

    0 讨论(0)
提交回复
热议问题