what are comments in Ruby language? How do you write a comment in Ruby?

Comments are a description of a line or block of a code about it.

Generally, Comments are for programmers not for a ruby interpreter. At runtime, These are ignored by Ruby.

In Ruby, You can write a comment in different following ways

  • Single line comments
  • Multi-Line comments
  • Shebang commands

It is always good to practice adding comments to the below blocks or lines of code

  • Classes description
  • before the method declaration
  • Any complex logic code
  • documentation comments

Single Line comments in Ruby

These are also called line-level comments.

  • It always starts with a pound (#) symbol character and ends with a line break.
  • There is a space after a pound symbol
  • a string of text that begins with a # symbol is ignored by the ruby compiler.
  • It is a description or piece of text for a single line of code.
  • These comments are not required at the start at the beginning of the line but also be write in the middle or end of the line Syntax:
\# These are single-line comments in the ruby file

Example

hello.rb

\# This is a simple hello world program in ruby
puts "Hello world program"
puts "Thanks" # can also be written here also

Learned single-line comments, How do you write multi-line comments

Multi-line comments in Ruby

These comments are written in multiple lines and are also called block comments. Hello World program is the first step to learning any programming language Steps to write a Hello World First program code.

Here is a way we can write a multi-line comment with the #\ symbol. Each line starts with a #\ character.

#\ multi-line comments 1
#\ multi-line comments 2
#\ multi-line comments 3

Ruby provides special syntax to write block-level comments

=begin
This comment is a multi-line comment 1
This comment is a multi-line comment 2
This comment is a multi-line comment 3
=end

This allows you to write multiple lines of code and be ignored by the compiler.

  • This always starts with =begin and ends with =end string span in multiple lines
  • There is space between = and begin or = and end

Block-level comments in Ruby Example:

=begin
Hello world sample program
Basic program to print hello world to console
First code to write in Ruby language
=end
puts "Hello world sample program"

Shebang comments

Shebang comments are special and different comments in shell-based OS like Unix and Linux.

  • It tells the ruby loader which command to execute this file
  • It tells the interpreter to tell about this ruby file.
  • It is always at the start of the line
  • These comments always start with #! characters
  • optional comments
  • It is not a ruby-provided syntax but bash or shell scripting syntax written in ruby files

Syntax

#!/usr/bin/env ruby

Normally you run ruby files with ruby test.rb command

test.rb

#! /usr/local/bin/ruby -w
puts "Shebang comments example"

In this file, You will run the file using the./test.rb command in Linux, where. represents the current directory.

./test.rb

Once you run the above command, It looks for the first line of what command to run, hence it translates to the following command and runs the program

/usr/local/bin/ruby -w ./test.rb

It simplifies the running ruby program from the command line without providing a ruby-interpreter command.

Magic comments

Magic comments are special comments to run ruby interpreter with special functionality.

There are different properties to add to the program

  • frozen_string_literal
  • encoding
  • warn_indent

These comments start with the # symbol and are followed by space and magic properties separated by a colon(:).

Here are examples of magic comments in ruby

frozen_string_literal tells to compiler by setting true or false and enables the string frozen frozen_string_literal_comments.rb

# frozen_string_literal:true

Here is an example to change file encoding to a new value, Default encoding is UTF-8. It enables the interpretation of the file with the new encoding.

# encoding: iso-8859-2

warn_indent tells the compiler to give a warning when indentation is not correct.

# warn_indent: true

It gives an error warning: mismatched indentations at '' with '' at 2 for the ruby file.

Conclusion

You learned different types of comments in Ruby

  • single-line comments (#)
  • Multiple line comments (#begin and #end)
  • Shebang comments (#!)
  • Magic comments (# encoding)