什么是<< – ruby?

从Ruby_Newbie指南到符号:

作者试图展示attr_writer方法的简化实现。

#!/usr/bin/env ruby def make_me_a_setter(thename) eval <<-SETTERDONE # <----- Here def #{thename}(myarg) @#{thename} = myarg end SETTERDONE end class Example make_me_a_setter :symboll make_me_a_setter "stringg" def show_symboll puts @symboll end def show_stringg puts @stringg end end example = Example.new example.symboll("ITS A SYMBOL") example.stringg("ITS A STRING") example.show_symboll example.show_stringg 

这是一个heredoc 。 从“ Here Documents ”文档中:

如果您正在编写大块文本,可以使用“here document”或“heredoc”:

 expected_result = < 

heredoc开始于<之后的行,并以HEREDOC开头的下一行HEREDOC 。 结果包括结束换行符。

这是一个多行字符串。 代码评估嵌入在字符串中的代码。 有关多行字符串的更多信息

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

PS不建议使用eval,alternative – yield,instance_eval,class_eval。