Ruby:用于字符串插值和注释的语法(Ruby: syntax for string interpolation and commenting)

编程入门 行业动态 更新时间:2024-10-28 02:29:29
Ruby:用于字符串插值和注释的语法(Ruby: syntax for string interpolation and commenting)

这个问题有很多类似的问题,但我认为没有人真的有我需要的答案。

根据我的理解,任何在其之前有#东西都会成为评论,如下所示:

puts "not a comment" # puts "in a comment"

但是当涉及到字符串插值时,您可以说:

puts "time is #{Time.now}"

并且#{Time.now}中的#{Time.now}不会开始评论它后面写的任何内容。

这是怎么回事?

There are a lot of similar questions to this one but i think none really had the answer i was in need of.

From what i perceived, anything that has a # preceding it becomes a comment, as in:

puts "not a comment" # puts "in a comment"

but when it come to string interpolation, you can say:

puts "time is #{Time.now}"

and the # at #{Time.now} doesn't start commenting whatever is written after it.

How is it so?

最满意答案

在这种情况下,#符号用引号括起来,并作为字符串的一部分进行分析,而不是作为注释。

The # sign in this case is encased in quotation marks, and is parsed as part of the string, not as a comment.

更多推荐

本文发布于:2023-08-02 14:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1376997.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   注释   语法   插值   Ruby

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!