开发者

What is the equivalent of a Python docstring in Ruby?

开发者 https://www.devze.com 2023-02-10 20:29 出处:网络
In Python, you can access an object\'s docstri开发者_如何学Gong by using obj.__doc__. What is the equivalent action in Ruby?Ruby does not have a Python __doc__ equivalent. They often use Rdoc Formatfo

In Python, you can access an object's docstri开发者_如何学Gong by using obj.__doc__. What is the equivalent action in Ruby?


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


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.


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.


I don't believe ruby supports this.

0

精彩评论

暂无评论...
验证码 换一张
取 消