what are comments in Ruby language? How do you write a comments in Ruby?
Comments are description about line or block of a code about it.
Generally Comments are for programmers not for ruby interpreter. At runtime, These are ignored by Ruby.
In Ruby, You can write a comments in different following ways
- Single line comments
- Multi Line comments
- Shebang commands
It is always good practise to add Comments to the below blocks or lines of code
- Classes description
- before method declaration
- Any complex logic code
- documentation comments
Single Line comments
These also called line level comments.
- It always starts with pound (#) symbol character and ends with line break.
- There is an space after a pound symbol
- an string of text begins with # symbol are ignored by ruby compiler.
- It is an description or piece of text for single line of code.
- These comments are not required at starts in begin of line, but also can write in middle or end of line Syntax:
\# This is single line comments in ruby file
\# This is simple hello world program in ruby puts "Hello world program" puts "Thanks" # can also written here also
Learned single line comments, How do you write a multi line comments
Multi line comments
These comments written in multiple line and also called block comments. Hello World program is a first step to learn any programming language Steps to write a Hello World First program code.
Here is a way we can write a multi line comments with #\ symbol. Each line starts with #\ character.
#\ multi line comments 1 #\ multi line comments 2 #\ multi line comments 3
Ruby provides special syntax to write a 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 a multiple lines of code and ignored by compiler.
- This always starts with
=beginand ends with
=endstring 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 are special and different comments in shell based OS like Unix and Linux.
- It tells ruby loader which command to execute this file
- It tells interpreter to tell about this ruby file.
- It is always at starting of line
- These comments always starts with
- optional comments
- It is not an ruby provides syntax but bash or shell scripting syntax written in ruby files
Normally you run ruby files with
ruby test.rb command
#! /usr/local/bin/ruby -w puts "Shebang comments example"
In this file, You will run the file using
./test.rb command in linux, where . represents current directory.
Once you run above command, It looks for first line what command to run, hence it translates to following command command and runs the program
/usr/local/bin/ruby -w ./test.rb
It simplifies the running ruby program from command line without providing ruby interpreter command
Magic comments are special comments to run ruby interpreter with special functionality.
There are different properties to add in program
These comments starts with
# symbol and followed by
space and magic properties separated by colon(:).
Here are an examples for magic comments in ruby
frozen_string_literal tells to compiler by setting true or false and enables string frozen
Here is an example to change file encoding to new value, Default encoding is
UTF-8. It enables to interpret the file with new encoding.
# encoding: iso-8859-2
warn_indent tells compiler to give warning when indentation is not correct.
# warn_indent: true
It gives an error
warning: mismatched indentations at '' with '' at 2 for ruby file.
You learned different types of comments in Ruby
- single line comments (#)
- Multiple line comments (#begin and #end)
- Shebang comments (#!)
- Magic comments (# encoding)