Multi-Line Comments in Ruby?

后端 未结 10 1625
忘了有多久
忘了有多久 2020-11-30 16:21

How can I comment multiple lines in Ruby?

相关标签:
10条回答
  • 2020-11-30 16:50
    =begin
    My 
    multiline
    comment
    here
    =end
    
    0 讨论(0)
  • 2020-11-30 16:51
    #!/usr/bin/env ruby
    
    =begin
    Every body mentioned this way
    to have multiline comments.
    
    The =begin and =end must be at the beginning of the line or
    it will be a syntax error.
    =end
    
    puts "Hello world!"
    
    <<-DOC
    Also, you could create a docstring.
    which...
    DOC
    
    puts "Hello world!"
    
    "..is kinda ugly and creates
    a String instance, but I know one guy
    with a Smalltalk background, who
    does this."
    
    puts "Hello world!"
    
    ##
    # most
    # people
    # do
    # this
    
    
    __END__
    
    But all forgot there is another option.
    Only at the end of a file, of course.
    
    • This is how it looks (via screenshot) - otherwise it's hard to interpret how the above comments will look. Click to Zoom-in:

    0 讨论(0)
  • 2020-11-30 16:53
      def idle
        <<~aid
        This is some description of what idle does.
    
        It does nothing actually, it's just here to show an example of multiline
        documentation. Thus said, this is something that is more common in the
        python community. That's an important point as it's good to also fit the
        expectation of your community of work. Now, if you agree with your team to
        go with a solution like this one for documenting your own base code, that's
        fine: just discuss about it with them first.
    
        Depending on your editor configuration, it won't be colored like a comment,
        like those starting with a "#". But as any keyword can be used for wrapping
        an heredoc, it is easy to spot anyway. One could even come with separated
        words for different puposes, so selective extraction for different types of
        documentation generation would be more practical. Depending on your editor,
        you possibly could configure it to use the same syntax highlight used for
        monoline comment when the keyword is one like aid or whatever you like.
    
        Also note that the squiggly-heredoc, using "~", allow to position
        the closing term with a level of indentation. That avoids to break the visual reading flow, unlike this far too long line.
        aid
      end
    

    Note that at the moment of the post, the stackoverflow engine doesn't render syntax coloration correctly. Testing how it renders in your editor of choice is let as an exercise. ;)

    0 讨论(0)
  • 2020-11-30 16:56
    #!/usr/bin/env ruby
    
    =begin
    Between =begin and =end, any number
    of lines may be written. All of these
    lines are ignored by the Ruby interpreter.
    =end
    
    puts "Hello world!"
    
    0 讨论(0)
提交回复
热议问题