DISQUS

Life is grand: Commenting in Ruby

  • Farrel Lifson · 3 years ago
    Pretty much most people don't even use the =begin/=end blocks any more. I just write stuff like

    # This function adds two numbers together.
    # No overflow checking is provided.
    def sum(a,b)
    a + b
    end

    It's easier to read and makes the comments more discernible.
  • Paul Watson · 3 years ago
    In normal code I agree, line comments work better. But in this case, and often enough, I had 15 lines of code I wanted to exclude to check something.

    Block comments are useful during development.
  • Matt Law · 2 years ago
    If you use textmate on the mac, you can highlight a block of code and hit command + / to comment it and then again to remove the comments. Or on vim shift + v and cursor down to select the line then s/^/#/g, phew!

    I agree though, it can be painful after spending years using /* */ comments. IMHO, its one of the very few annoyances I have with ruby.
  • Jay · 2 years ago
    Depending on your editor, you can also use the # comment easily to block comment by using rectangular select, eg in jedit alt+/ will toggle triangular select, then shift+down arrow at the beginning of the lines you want to comment, then a single # will put a hash at the beginning of all the lines. Similarly to uncomment, simply delete the # using the same select method. Once you get the hang of it, it's really easy.

    PS the rectangular select is brilliant, once you start using it, you'll wonder how you ever managed without it!
  • Brian · 2 years ago
    Do you know if there is any way to do block comment on view block? I mean on the rhtml file?

    Thanks
  • Los Angeles Electricians · 2 years ago
    Brian,
    For block comment, you do:

    =begin
    Your code
    =end

    I hope you find that useful
    Thanks
  • paulmwatson · 2 years ago
    Does that work in RHTML/ERB files though Los Angeles Electricians?
  • Los Angeles Electricians · 2 years ago
    No, That does not work.
    In Rhtml or ERB I would use :
    <!-- -->

    Do you have alternatives?