字符串块上的参数(<<< -BLOCK,param1,param2)是什么意思?

时间:2012-03-23 13:54:44

标签: ruby-on-rails ruby

我正在https://github.com/plataformatec/devise阅读一些源代码并找到代码行:

class_eval <<-URL_HELPERS, __FILE__, __LINE__ + 1

params __FILE____LINE__ + 1在块声明中做了什么(没有这些参数的字符串块的关系有什么变化)?

https://github.com/plataformatec/devise/blob/master/lib/devise/controllers/url_helpers.rb#L47

感谢

2 个答案:

答案 0 :(得分:5)

这些参数属于class_eval方法,而不属于here document。通常的做法是确保可以引发错误代码的错误将通过引用当前文件和正确的行号来显示。

答案 1 :(得分:2)

通过另一个显示HEREDOC如何工作的例子,我在IRB的另一天写道:

require 'nokogiri'
doc = Nokogiri.XML(<<ENDXML,&:noblanks)
  ...gobs and gobs of pasted xml...
ENDXML

甚至更疯狂的是一次传递多个HEREDOC字符串的合法语法:

p( <<END1, <<-END2, <<END3 )
  This indented text is part of
  the first parameter, END1
END1
And this text is part of param2
  whose ending sigil may be indented
  END2
and finally this text
is part of
the third parameter
END3
#=> "  This indented text is part of\n  the first parameter, END1\n"
#=> "And this text is part of param2\n  whose ending sigil may be indented\n"
#=> "and finally this text\nis part of\nthe third parameter\n"