Comments
A comment is a note for humans that starts with a hash mark and is ignored by Ruby. It is like a sticky note in the margin of a recipe.
In this page:
Syntax
# single-line comment
value = 1 # trailing comment
=begin
block comment
=end
Comments
A # starts a single-line comment in Ruby, and everything after it on that line is ignored. Multi-line comments use =begin and =end, though single-line # comments are far more common in practice.
Comments should explain non-obvious reasoning, not restate what the code already makes clear.
Note:
Most Ruby style guides discourage
=begin/=end blocks in favor of multiple # lines.Example: Comments
# This script demonstrates comments
puts "Hello!" # inline comment after code
=begin
This is a multi-line comment block,
though single-line # comments are more idiomatic.
=end
puts "Still running"
Login to try C/C++/Java/PHP code in the editor
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
Common Mistakes
- Using
//for comments, which is a syntax error in Ruby; use#. - Putting
=beginand=endafter spaces, when they must start at the very beginning of the line. - Forgetting
=end, which comments out the rest of the file.
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: