<<DESC 在 ruby​​ 中是什么意思? [英] What does <<DESC mean in ruby?

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

问题描述

我正在学习Ruby,在我使用的书中,有这样的示例代码

I am learning Ruby, and in the book I use, there is an example code like this

#...
restaurant = Restaurant.new
restaurant.name = "Mediterrano"

restaurant.description = <<DESC
One of the best Italian restaurants in the Kings Cross area, 
Mediterraneo will never leave you disappointed
DESC

#...

有人可以向我解释一下上面示例中 <<DESC 的含义吗?它和普通的字符串双引号有什么区别?

Can someone explain to me what <<DESC means in the above example? How does it differ from the common string double quote?

推荐答案

用于创建多行字符串.基本上,'<<DESC' 告诉 ruby​​ 考虑接下来的所有内容,直到下一个 'DESC' 关键字.DESC"不是强制性的,因为它可以用其他任何东西代替.

It is used to create multiline strings. Basically, '<< DESC' tells ruby to consider everything that follows until the next 'DESC' keyword. 'DESC' is not mandatory, as it can be replaced with anything else.

a = <<STRING
Here
is
a
multiline
string
STRING

<<运算符后跟一个标识符,用于标记文档的结尾.结束标记称为终止符.终止符之前的文本行连接在一起,包括换行符和任何其他空格.http://en.wikibooks.org/wiki/Ruby_Programming/Here_documents

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

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