Guide to Using Comment Lines in Ruby
Comments are lines that are not executed in Ruby programs but explain the purpose of the code. This guide teaches you how to write comments, when to use them, and best practices.
🧠 What Will You Learn in This Guide?
- Writing single line and multi line (block) comments in Ruby
- How and when to use inline comments
- Debugging with comment lines
- Temporarily disable sections while developing code
🔹 1. Commenting Basics in Ruby
In Ruby, comment lines start with the character # and continue until the end of the line.
# Bu bir Ruby yorumudur.
puts "Merhaba Dünya!"
💡 It is recommended to leave a space after the # sign to increase readability.
Comments are completely ignored by the Ruby interpreter; It is for developers only.
To add a comment in a simple program:
# Kullanıcıdan adını alır
gets.chomp
# Ekrana merhaba mesajı yazdırır
puts "Merhaba!"
🔹 2. Code Explanation Example
The following example converts an array to an HTML list. Comments explain what each step does.
sharks = ['hammerhead', 'great white', 'dogfish']
# Her elemanı HTML listesi haline getirir
listitems = sharks.map { |shark| " <li>#{shark}</li>\n" }
# Tamamlanmış <ul> etiketiyle yazdırır
print "<ul>\n#{listitems.join}</ul>"
💬 Comments explain the purpose of the code but do not affect its operation.
🔹 3. Indented Comments
Comments must be in the same indent as the code they comment on:
class Eightball
# Cevap seçeneklerini oluşturur
def initialize
@choices = ["Evet", "Hayır", "Belki"]
end
# Rastgele bir cevap döndürür
def shake
@choices.sample
end
end
💡 Indent alignment improves readability, especially in classes and methods.
🔹 Block 4 (Multi-Line) Comments
For long descriptions you can put # on each line:
# Bu blok yorum, birkaç satır boyunca
# kodun genel amacını açıklar.
# Özellikle karmaşık fonksiyonlarda tercih edilir.
Alternatively =begin and =end can be used:
=begin
Bu bir çok satırlı yorumdur.
Her satır başına `#` koymaya gerek yoktur.
=end
⚠️ This method only works at the beginning of a line, it gives an error on indented lines.
🔹 5. Inline Comments
You can make a short description at the end of a line:
pi = 3.14 # Pi sabitinin yaklaşık değeri
💡 Inline comments should be short and concise.
Useful for explaining complex operations when working with multiple people:
a = Complex(4, 3) # 4 + 3i karmaşık sayısını oluşturur
🔹 6. Temporarily Disable Lines of Code
If you don't want to run some lines during testing or debugging, you can comment them out:
# puts "Bu satır çalışmayacak"
puts "Bu satır çalışacak"
In a game example:
if cevap == 'y'
# play # Tekrar oynamayı devre dışı bırak
else
exit
end
💬 This technique is used to temporarily disable faulty lines while testing.
💡 Best Commenting Practices
- Explain why the code does, not what it does.
- Avoid unnecessary or repetitive comments.
- Keep comments updated with code; Old comments are confusing.
- Use block comments for long explanations and inline comments for short notes.
💬 Frequently Asked Questions (FAQ)
1️⃣ Do Ruby comments slow down the program? No. The Ruby interpreter completely ignores comment lines.
2️⃣ How long can inline comments be? It should be short and single line; otherwise it reduces readability.
3️⃣ Is there another method to exclude part of the code from testing? Apart from the comment line, you can use conditional statements (if false).
4️⃣ Why is =begin / =end not recommended? It is rarely preferred by modern Ruby developers because it does not work with indentation.
5️⃣ Who should read comments? Both your teammates and your future self. Great for remembering the reason for the code.
🎯 Result
Comments in Ruby make your code more readable and collaborative. Use comments effectively to document code, temporarily disable sections during testing, and make it easier for your team to understand.
💡 Try: Open a Ruby project on GenixNode and practice different comment types.

