Ruby 中的 Python 文档字符串相当于什么?

发布于 2024-10-17 15:49:09 字数 64 浏览 3 评论 0原文

在 Python 中,您可以使用 obj.__doc__ 来访问对象的文档字符串。 Ruby 中的等效操作是什么?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

深海夜未眠 2024-10-24 15:49:09

Ruby 没有 Python __doc__ 等效项。他们经常使用 Rdoc 格式作为文档,例如:

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

  # A vertical position.
  def y
  end
end

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
知你几分 2024-10-24 15:49:09

不幸的是,Ruby 没有任何像 Python 一样的内置文档字符串。

RDoc 看起来很糟糕。 RDoc 被设计为处理成 HTML 格式并在我们的浏览器中读取。它不是纯文本。谁喜欢阅读类似 HTML 的源代码?院子更好。还有 TomDoc 仅使用纯文本。但它们都无法与 Pythonic 文档字符串相比,例如,Pythonic 文档字符串允许从任何 python 控制台进行简单的自动完成,并且不需要使用任何处理工具。

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.

以往的大感动 2024-10-24 15:49:09

使用 Yard 在 Ruby 中记录代码更容易,Yard 支持不同的标签,例如 :NODOC:

要使用 Yard 记录代码,只需将注释写在代码上方即可。

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

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

然后在项目的当前工作目录上运行 yard ,这将为您生成 $PWD/doc 目录,其中包含一组不错的文档。

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.

猫九 2024-10-24 15:49:09

我不相信红宝石支持这一点。

I don't believe ruby supports this.

内心旳酸楚 2024-10-24 15:49:09

Ruby 命名为 HEREDOCS,支持各种格式选项,例如文字、前导空格等。我发现的两篇有用的文章是:

下面是一些简单示例:

irb(main):001:0" puts <<~TILDE
irb(main):002:0"   This removes leading whitespace to the left
irb(main):003:0"     but preserves indentation underneath it,
irb(main):004:0"     which is quite helpful for formatting.
irb(main):005:0"
irb(main):006:0> TILDE
This removes leading whitespace to the left
  but preserves indentation underneath it,
  which is quite helpful for formatting.
irb(main):007:0" puts <<-LITERAL
irb(main):008:0"     This will do whatevery you
irb(main):009:0"    tell it to do
irb(main):010:0"         which can be nice if you want
irb(main):011:0"      things to be laid out very, very
irb(main):012:0"        p r e c i s e l y
irb(main):013:0"     ... except the problem is you have
irb(main):014:0"       to put this text all the way to the left
irb(main):015:0"     of your code if you want to avoid leading
irb(main):016:0"     whitespace.
irb(main):017:0> LITERAL
    This will do whatevery you
   tell it to do
        which can be nice if you want
     things to be laid out very, very
       p r e c i s e l y
    ... except the problem is you have
      to put this text all the way to the left
    of your code if you want to avoid leading
    whitespace.

HEREDOCS 还支持字符串插值。

irb(main):018:0> name = 'Jeff'
=> "Jeff"
irb(main):019:0" puts <<~INTERPOLATION
irb(main):020:0"   My name is #{name}
irb(main):021:0> INTERPOLATION
My name is Jeff

Ruby has named HEREDOCS that support various formatting options such as literal, leading whitespace, and others. Two useful articles I found on this are:

Below are some quick examples:

irb(main):001:0" puts <<~TILDE
irb(main):002:0"   This removes leading whitespace to the left
irb(main):003:0"     but preserves indentation underneath it,
irb(main):004:0"     which is quite helpful for formatting.
irb(main):005:0"
irb(main):006:0> TILDE
This removes leading whitespace to the left
  but preserves indentation underneath it,
  which is quite helpful for formatting.
irb(main):007:0" puts <<-LITERAL
irb(main):008:0"     This will do whatevery you
irb(main):009:0"    tell it to do
irb(main):010:0"         which can be nice if you want
irb(main):011:0"      things to be laid out very, very
irb(main):012:0"        p r e c i s e l y
irb(main):013:0"     ... except the problem is you have
irb(main):014:0"       to put this text all the way to the left
irb(main):015:0"     of your code if you want to avoid leading
irb(main):016:0"     whitespace.
irb(main):017:0> LITERAL
    This will do whatevery you
   tell it to do
        which can be nice if you want
     things to be laid out very, very
       p r e c i s e l y
    ... except the problem is you have
      to put this text all the way to the left
    of your code if you want to avoid leading
    whitespace.

HEREDOCS also support string interpolation.

irb(main):018:0> name = 'Jeff'
=> "Jeff"
irb(main):019:0" puts <<~INTERPOLATION
irb(main):020:0"   My name is #{name}
irb(main):021:0> INTERPOLATION
My name is Jeff
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文