What is <<- in ruby?

前端 未结 2 587
隐瞒了意图╮
隐瞒了意图╮ 2021-01-21 06:03

From the Ruby_Newbie Guide to Symbols:

Author is trying to show a simplified implementation of the attr_writer method.

#!/usr/bin/env ruby

def make_me_a         


        
相关标签:
2条回答
  • 2021-01-21 06:18

    It's a heredoc. From the "Here Documents" documentation:

    If you are writing a large block of text you may use a “here document” or “heredoc”:

    expected_result = <<HEREDOC
    This would contain specially formatted text.
    
    That might span many lines
    HEREDOC
    

    The heredoc starts on the line following <<HEREDOC and ends with the next line that starts with HEREDOC. The result includes the ending newline.

    0 讨论(0)
  • 2021-01-21 06:31

    It's a multi line string. The code evaluates code embedded inside string. More on multi line strings:

    http://blog.jayfields.com/2006/12/ruby-multiline-strings-here-doc-or.html

    P.S. Using eval is not recommended, alternatives - yield, instance_eval, class_eval.

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