0
0
Rubyprogramming~20 mins

YARD for documentation in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
YARD Documentation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this YARD documentation comment parsing?
Given the following Ruby method with YARD comments, what will be the description extracted by YARD for the method `greet`?
Ruby
##
# Greets a person with their name.
#
# @param name [String] the name of the person
# @return [String] greeting message
#
def greet(name)
  "Hello, #{name}!"
end
A"Hello, #{name}!"
B"greet method"
C"Greets a person with their name."
D"@param name [String] the name of the person"
Attempts:
2 left
💡 Hint
Look at the first comment line after the ## marker.
🧠 Conceptual
intermediate
1:30remaining
Which YARD tag documents the return type of a method?
In YARD documentation, which tag is used to specify what a method returns?
A@return
B@param
C@yield
D@example
Attempts:
2 left
💡 Hint
It describes the output of the method.
🔧 Debug
advanced
2:30remaining
Why does this YARD comment fail to document the method parameter?
Examine the following YARD comment and method. Why will YARD not recognize the parameter documentation correctly? ## # Calculates the square of a number. # @param num the number to square # @return [Integer] the squared result # def square(number) number * number end
Ruby
##
# Calculates the square of a number.
# @param num the number to square
# @return [Integer] the squared result
#
def square(number)
  number * number
end
AThe comment block is missing the initial # marker.
BThe @param tag is missing the type specification in brackets.
CThe method is missing a return statement.
DThe parameter name in @param does not match the method parameter name.
Attempts:
2 left
💡 Hint
Check if the parameter names match exactly.
📝 Syntax
advanced
2:00remaining
Which YARD comment block is syntactically correct for documenting a method?
Choose the correctly formatted YARD comment block for this method: def add(a, b) a + b end
A
## Adds two numbers.
@param a [Integer]
@param b [Integer]
@return [Integer]
B
##
# Adds two numbers.
# @param a [Integer]
# @param b [Integer]
# @return [Integer]
C
# Adds two numbers.
# @param a Integer
# @param b Integer
# @return Integer
D
##
# Adds two numbers.
@param a [Integer]
@param b [Integer]
@return [Integer]
Attempts:
2 left
💡 Hint
YARD comment blocks start with ## and each line begins with #.
🚀 Application
expert
3:00remaining
How many methods will YARD document in this Ruby class?
Consider this Ruby class with YARD comments. How many methods will YARD generate documentation for? class Calculator ## # Adds two numbers # @param x [Integer] # @param y [Integer] # @return [Integer] def add(x, y) x + y end # Subtracts y from x def subtract(x, y) x - y end ## # Multiplies two numbers # @param x [Integer] # @param y [Integer] # @return [Integer] def multiply(x, y) x * y end end
Ruby
class Calculator
  ##
  # Adds two numbers
  # @param x [Integer]
  # @param y [Integer]
  # @return [Integer]
  def add(x, y)
    x + y
  end

  # Subtracts y from x
  def subtract(x, y)
    x - y
  end

  ##
  # Multiplies two numbers
  # @param x [Integer]
  # @param y [Integer]
  # @return [Integer]
  def multiply(x, y)
    x * y
  end
end
A2
B3
C1
D0
Attempts:
2 left
💡 Hint
YARD only documents methods with comment blocks starting with ##.