简体   繁体   English

是否可以更改红宝石评论符号?

[英]Is it possible to change ruby comment symbol?

So, to comment in ruby, you need the # symbol. 所以,要在ruby中发表评论,你需要#符号。

# this is some comments

Which is fine, but for multi-line comments, ruby has an ugly system. 这很好,但对于多行注释,ruby有一个丑陋的系统。

=begin
    comment line 1
    comment line 2
=end

I have search around the internet and found nothing on the topic. 我在互联网上搜索,没有发现任何主题。 I want to see if I am able to change that format to something better by defining my own commenting system. 我想通过定义自己的评论系统来看看我是否能够将这种格式更改为更好的格式。 Such as: 如:

/*
    comment line 1
    comment line 2
*/

I want to see if I can do something like 我想看看我能做些什么

def /*
  define comment logic
end
def */
  define comment logic
end

Just something to that effect. 就是那种效果。 I don't need to replace to current one, just want to see how I can define my own. 我不需要替换当前的,只是想看看我如何定义自己的。 I'm not looking to rewrite ruby. 我不打算重写ruby。 I just want to see if there is something simple that i can do whenever I write ruby. 我只是想看看每当我写ruby时我能做些什么都很简单。 As an example, if I want to add a method to the String class, I can do 举个例子,如果我想在String类中添加一个方法,我可以这么做

class String
    def new_method
        # some new functionality.
    end
end

I want to see if I can do something like that for comments. 我想看看我是否可以做类似的评论。

Nobody uses multi-line syntax. 没有人使用多行语法。 People do this instead: 人们这样做:

# comment line 1
# comment line 2

Most editors have a shortcut that allows one to comment in multiple lines easily. 大多数编辑器都有一个快捷方式,允许用户轻松地在多行中进行评论。 You will get used to it! 你会习惯的!

A comment says "Ruby stops here, what follows is outside of Ruby". 评论说“Ruby停在这里,接下来是在Ruby之外”。 Therefore, it should be pretty obvious, that you cannot change what a comment is from inside Ruby. 因此,它应该是很明显的,你不能改变一个评论是从红宝石什么。

But there's another problem with your proposed syntax: it is already valid Ruby. 但是你提出的语法还有另一个问题:它已经是有效的Ruby了。 It's a multi-line Regexp literal. 这是一个多行的Regexp文字。 (Yes, it is semantically invalid, but it is a syntactically valid Regexp literal.) (是的,它在语义上是无效的,但它一个语法上有效的Regexp文字。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM