Anyone Can Comment This Ruby Code

How to comment code in Rails views?

<% code code # comment %> USED to work but I think that was accidental.

You were always supposed to put comments in separate comment tags <%# comment %>

Note NO SPACE before the pound.

Now the old loophole is closed (I forget whether 'now' means Ruby 1.8 or Rails 3 or what) so that:

<% code code  #  this runs too %>
<% # also runs %>
<%# the only way to comment out %>

Inline comments in Ruby

No, Ruby does not have inline comments.

Comments of this style have a tendency to reduce readability, since they makes the code harder to follow.

In your case it would be best to split your array items into separate rows and comment out the one row.

my_array = ['first',
# 'second',
'third',
'fourth']

Multi-Line Comments in Ruby?

#!/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:

Comments in a text-editor

Can comments be used in the .gemspec file?

I'm making a Ruby gem, and I want to put comments in the file. Would this be allowed, or would it mess up the gem?

Like most programming languages, Ruby has comments. In fact, Ruby has two kinds of comments:

  • Single-line comments start with the token # and end with the end of the line:
    #!/usr/bin/env ruby
    # Shebang lines are just interpreted as comments, which is clever.
    some_code # This is a comment.

    # So is this.

    foo.bar.
    # Comments are allowed in lots of places.
    baz(
    # And here.
    23, # And here.
    42,
    # And here.
    :foo
    )
    # Even here.
    .quux
  • Multi-line comments start with the token =begin at the beginning of a line and end with the token =end at the beginning of a line:
    some_code
    =begin This is a comment.
    This is still the same comment.
    So is this.
    This is not the end of the comment: =end

    But this is:
    =end

If you want to know all the gory details, I recommend reading:

  • ISO/IEC 30170:2012 Information technology — Programming languages — Ruby specification, section 8.5 Comments
  • language/comment_spec.rb of The Ruby Spec Suite aka ruby/spec
  • Section 2.1 Lexical structure of The Ruby Programming Language by David Flanagan and Yukihiro 'matz' Matsumoto
  • The Code Comments sub-section in the Ruby Syntax section of the Ruby documentation
  • The introductory parts of Programming Ruby by Dave Thomas, Andy Hunt, and Chad Fowler

Ruby Code Comment Within Quotes

MySQL does support comment syntax so your code should work as is. However, I would prefer to use a "heredoc":

mysql.query <<END
CREATE TABLE If NOT EXISTS #{table}(
application varchar(255),
eventType varchar(255),
eventTs datetime,
eventDayWeek int,
newColumnHere int, #Hello, I would like to be a comment
eventHourDay int,

....)
END

Block commenting in Ruby

You can do

=begin
[Multi line comment]
=end

=begin and =end must be at the beginning of the line (not indented at all).

Source

Also, in TextMate you can press Command + / to toggle regular comments on a highlighted block of code.

Source



Related Topics



Leave a reply



Submit