What is the equivalent of a Python docstring in Ruby?

Question:

In Python, you can access an object’s docstring by using obj.__doc__. What is the equivalent action in Ruby?

Asked By: parzival

||

Answers:

I don’t believe ruby supports this.

Answered By: Olly

Ruby does not have a Python __doc__ equivalent. They often use Rdoc Format for documentation, for example:

# For example, a square or circle.
class Shape
  # A horizontal position.
  def x
  end

  # A vertical position.
  def y
  end
end
Answered By: Tarantula

It’s easier to document in Ruby using Yard, which supports different tags like :NODOC:

To document your code with Yard, just write the comment above your code.

# MyClass.new(...) some comment here
class MyClass
end

# foo("bar")
def foo(bar = nil)
end

then run yard on the current working directory of your project, this will generate the $PWD/doc directory for you with a nice set of documentations.

Answered By: jjuliano

Unfortunatelly Ruby does not have any Python like built-in docstrings.

RDoc looks terrible. RDoc is designed to be processed into HTML format and read in the we browser. It’s not plain text. Who likes reading HTML-like source code? YARD is better. There is also TomDoc which use just plain text. But none of them compare to Pythonic docstrings which e.g. allow for simple autocompletion from any python console and do need to use any processing tool.

Answered By: hipertracker