English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Ruby Comments

Comments are comment lines within Ruby code that are ignored at runtime. Single-line comments start with the # character and end at the end of the line, as shown below:

Online Examples

#!/usr/bin/ruby -w
 
# This is a single-line comment.
 
puts "Hello, Ruby!"

When executed, the above program will produce the following results:

Hello, Ruby!

Ruby Multi-line Comments

You can use =begin and =end Syntax comments can be multi-line, as shown below:

Online Examples

#!/usr/bin/ruby -w
 
puts "Hello, Ruby!"
 
=begin
This is a multi-line comment.
It can be extended to any number of lines.
However, =begin and =end can only appear on the first and last lines. 
=end

When executed, the above program will produce the following results:

Hello, Ruby!

Make sure there is enough space between the end of the code and the comments so that it is easy to distinguish between comments and code. If there are more than one comment at the end, align them. For example:

Online Examples

@counter      # Track the number of times the page is clicked
@siteCounter  # Track the number of times all pages are clicked