什么是 <<- 在红宝石中? [英] What is <<- in ruby?

查看:38
本文介绍了什么是 <<- 在红宝石中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自 Ruby_Newbie 符号指南:

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

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

#!/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.来自此处文档" 文档:

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

如果你要写一大段文字,你可以使用here document"或heredoc":

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

heredoc 从 <<HEREDOC 之后的行开始,以下一行以 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.

这篇关于什么是 &lt;&lt;- 在红宝石中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆