YARD helps you write clear notes inside your Ruby code. These notes explain what your code does, so others (and you) can understand it easily later.
When you want to explain what a method or class does in your Ruby program.
When you want to create easy-to-read documentation for your Ruby project.
When you want to help teammates understand your code without reading every line.
When you want to generate HTML pages that show your code's documentation.
When you want to keep your code and its explanation together in one place.
Syntax
Ruby
# Use special comments starting with @ to describe code elements# @param name [Type] description# @return [Type] description# @example# example code here
YARD comments start with a # and use @tags like @param and @return to explain code parts.
You write these comments just before the method, class, or module you want to document.
Examples
This example shows how to document a simple method with parameters and return type.
Ruby
# Adds two numbers# @param a [Integer] the first number# @param b [Integer] the second number# @return [Integer] the sum of a and bdef add(a, b)
a + b
end
This example documents a class and its attribute.
Ruby
# Represents a person# @!attribute [r] name# @return [String] the person's nameclass Person
attr_reader :name
# Initialize with a name# @param name [String] the person's namedefinitialize(name)
@name = name
endend
This example includes an example usage inside the documentation.
Ruby
# Calculates factorial# @param n [Integer] number to calculate factorial for# @return [Integer] factorial of n# @example# factorial(5) #=> 120def factorial(n)
return1if n <= 1
n * factorial(n - 1)
end
Sample Program
This program defines a Calculator class with documented methods. It then creates an object and prints results of add and multiply.
Ruby
# Calculator class# Performs basic math operationsclass Calculator
# Adds two numbers# @param x [Integer] first number# @param y [Integer] second number# @return [Integer] sum of x and ydef add(x, y)
x + y
end# Multiplies two numbers# @param x [Integer] first number# @param y [Integer] second number# @return [Integer] product of x and ydef multiply(x, y)
x * y
endend
calc = Calculator.newputs calc.add(3, 4)
puts calc.multiply(3, 4)
OutputSuccess
Important Notes
You can generate HTML documentation by running yard doc in your project folder.
YARD supports many tags like @param, @return, @example, @see to make docs clear.
Keep your comments simple and focused on what the code does, not how it works internally.
Summary
YARD lets you write easy-to-read notes inside Ruby code to explain it.
Use special @tags in comments before methods or classes to describe parameters, returns, and examples.
You can generate nice HTML pages from these comments to share documentation.
Practice
(1/5)
1. What is the main purpose of using YARD in Ruby code?
easy
A. To automatically fix syntax errors in Ruby code
B. To speed up the execution of Ruby programs
C. To write clear documentation inside the code using special comments
D. To convert Ruby code into other programming languages
Solution
Step 1: Understand YARD's role
YARD is a tool to add documentation inside Ruby code using comments.
Step 2: Identify the correct purpose
It helps explain code clearly, not speed execution or fix errors.
Final Answer:
To write clear documentation inside the code using special comments -> Option C
Quick Check:
YARD -> documentation [OK]
Hint: YARD is for documentation, not code execution [OK]
Common Mistakes:
Thinking YARD runs or speeds up code
Confusing YARD with debugging tools
Assuming YARD converts code to other languages
2. Which of the following is the correct way to document a method parameter using YARD?
easy
A. # @param name [String] the user's name
B. // @param name String the user's name
C. /* @param name String the user's name */
D.
Solution
Step 1: Recognize Ruby comment style
Ruby uses # for single-line comments, so YARD tags must start with #.
Step 2: Identify correct YARD tag syntax
The correct format is "# @param name [Type] description".
Final Answer:
# @param name [String] the user's name -> Option A
Quick Check:
# -> Ruby comments [OK]
Hint: YARD tags start with # in Ruby code comments [OK]
Common Mistakes:
Using comment styles from other languages
Omitting square brackets around type
Writing tags inside HTML or block comments
3. What will be the output of the following YARD comment when generating documentation?
# Adds two numbers
# @param a [Integer] first number
# @param b [Integer] second number
# @return [Integer] sum of a and b
# @example
# add(2, 3) #=> 5
def add(a, b)
a + b
end
medium
A. Only the method name will appear without any details
B. Syntax error because @example is not allowed
C. The code will run and print 5
D. Documentation showing method description, parameters with types, return type, and example usage
Solution
Step 1: Understand YARD comment tags
The tags @param, @return, and @example describe method details for documentation.
Step 2: Recognize YARD output
YARD generates HTML docs showing description, parameters, return type, and examples.
Final Answer:
Documentation showing method description, parameters with types, return type, and example usage -> Option D
Quick Check:
YARD tags -> detailed docs [OK]
Hint: YARD tags create detailed docs, not runtime output [OK]
Common Mistakes:
Confusing documentation comments with code execution
Thinking @example causes errors
Expecting code output instead of docs
4. Identify the error in this YARD comment block:
# @param name String the user's name
# @return String greeting message
def greet(name)
"Hello, #{name}!"
end
medium
A. Missing square brackets around the type in @param and @return tags
B. The method name should be capitalized
C. YARD comments must be placed after the method definition
D. The string interpolation syntax is incorrect
Solution
Step 1: Check YARD tag syntax
YARD requires types in square brackets, e.g., [String], not just String.
Step 2: Verify other parts
Method name capitalization and comment placement are correct; string interpolation is valid Ruby.
Final Answer:
Missing square brackets around the type in @param and @return tags -> Option A
Quick Check:
[Type] -> YARD requirement [OK]
Hint: Always put types in square brackets in YARD tags [OK]
Common Mistakes:
Omitting square brackets around types
Placing comments after method instead of before
Confusing Ruby syntax errors with documentation errors
5. You want to document a Ruby class with YARD that has a method returning a hash with keys as symbols and values as arrays of strings. Which is the best way to write the @return tag for this method?
hard
A. # @return [Hash] a hash with keys and values
B. # @return [Hash{Symbol => Array<String>}] a hash mapping symbols to arrays of strings
C. # @return [Array<Hash>] an array of hashes
D. # @return [Hash(Symbol, Array)] a hash with symbol keys and array values
Solution
Step 1: Understand complex type syntax in YARD
YARD supports detailed type annotations like Hash{KeyType => ValueType}.
Step 2: Match the method return type
The method returns a hash with Symbol keys and Array of String values, so use Hash{Symbol => Array<String>}.
Final Answer:
# @return [Hash{Symbol => Array<String>}] a hash mapping symbols to arrays of strings -> Option B
Quick Check:
Hash{Key => Value} -> correct syntax [OK]
Hint: Use Hash{KeyType => ValueType} for detailed return types [OK]
Common Mistakes:
Using vague types like just Hash
Incorrect syntax like Hash(Symbol, Array)
Confusing array of hashes with hash of arrays
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.