/ ruby

Comments in Ruby and JavaScript

Ruby Comments

There are basically two types of comments in Ruby. And they work in the same way as follows:

The block comment

The comment block is created with the =begin and =end delimiters.

def welcome
  =begin
    This will print the welcome message.
    You can also add your custom message.
  =end
  puts "Welcome to ruby comments learning :)"
end

The line comment

This is the simple comment where you place an octothorpe(#) as the first non-whitespace character of the line, and everything else written is excluded from being interpreted by Ruby.

   def welcome
     # This will print the welcome message.
     puts "Welcome to ruby comments learning :)"
   end

The in-line comment

The in-line comment is when you comment code at the end of a statement.

   def welcome
     puts "Welcome to ruby comments learning :)"  # This will print the welcome message.
   end

Now calling this method will print output as: (for all above code snippets)

=> welcome
Welcome to ruby comments learning :)
nil

JavaScript Comments

JavaScript offers two types of commets

Block Comments:

Block comments are formed with /*...*/

function welcome() {
  /* This will print the welcome message.
  You can also add your custom message.*/
  console.log("Welcome to ruby comments learning :)");
}

The line comments

function welcome() {
  console.log("Welcome to ruby comments learning :)"); // This will print the welcome message.
}

Note: Block comments are not safe for commenting out blocks of code. For example:

/*
  var rm_a = /a*/.match(s);
*/

This causes a syntax error. So, it is recommended that /*..*/ comments be avoided and // comments be used instead.

Reference: "JavaScript: The Good Parts" Book

Of course, the best code is code that doesn’t rely on comments at all. But sometimes we still need them!

Swati Jadhav

I am a Rubyist, ROR developer, passionate about change and trying new things, both professionally and in general with life. Currently, working on Ember.js applications and fasci...

Read More