Comments in Ruby
Learn how to write clean, readable, and professional Ruby code using single-line, multi-line, and documentation comments.
Single-line Comments
Single-line comments start with `#` and are best for short explanations, reminders, or clarifying what a specific line does.
1. Basic Single-line Comment
Everything after `#` on that line is ignored by the Ruby interpreter.
2. Inline Comment (End of Line)
Inline comments are placed at the end of a line to explain the line quickly.
3. Debug Notes / TODOs
Common practice: using keywords TODO, FIXME, NOTE so they can be searched easily in editors.
Multi-line Comments
Ruby supports multi-line comments using `=begin` and `=end` markers. However, most Rubyists prefer multiple `#` lines for readability.
1. =begin / =end Block
`=begin` and `=end` must start at the very beginning of a line (no leading spaces).
2. Multiple # Lines (Preferred)
Most Ruby developers prefer using `#` on each line over `=begin/=end` for better readability and editor support.
Documentation Comments (YARD)
YARD is the standard documentation tool for Ruby. It uses `#` comments with special tags like `@param`, `@return`, and `@example` to generate API documentation.
1. Basic YARD Comment
Place a YARD comment directly above the method definition.
2. @example Tag
Use `@example` to show sample usage directly in the docs.