Comments in Ruby
Comments in Ruby are used to explain the code, and they are ignored by the Ruby interpreter during program execution. In simple words, comments are used to describe the code and hide the code from execution.
Ruby supports two types of comments, those are
- Single-line comments
- Multi-line comments
Single-line comments
A single-line comment begins with the # symbol. Everything written after # on that line is treated as a comment.
Syntax:# single line comment
Multi-line comments
Multi-line comments are used when we describe the code with multiple lines. Ruby supports multi-line comments using the =begin and =end keywords.
Syntax:=begin
This is a multi-line comment.
It can contain multiple lines.
Ruby ignores this entire block.
=end
Example
# This program displays a welcome message
puts "Welcome to Ruby Programming"
name = "Study Glance" # Store the name
puts name
=begin
Program Name: Addition
Purpose: To add two numbers
Language: Ruby
=end
a = 10
b = 20
res = a + b
puts res
Output:
ruby comments.rb
Welcome to Ruby Programming
Study Glance
30