2012-03-23 86 views

回答

5

這些PARAMS屬於class_eval方法,不以here document。通常的做法是,確保可以提高撤銷代碼的錯誤將以當前文件和正確的行號顯示。

+0

我的意思是params對字符串塊聲明的影響,而不是class_eval方法 – daniloisr 2012-03-23 14:10:26

+0

@daniloisr我已經更新了答案。 – 2012-03-23 14:18:56

+0

不錯,我無法想象。非常感謝你=) – daniloisr 2012-03-23 14:27:43

2

通過展示here文檔是如何工作的另一種例子,前些天在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"