【问题标题】:Ruby class#method in text, class.method in code snippetRuby 文本中的 class#method,代码片段中的 class.method
【发布时间】:2015-07-29 10:49:25
【问题描述】:

我刚刚了解了 Array.push(*more_than_one) 与 Array cmets 中使用 Class#Method 和在 code sn-p(下面的链接1)。

我已阅读有关 Java # 与 .在响应中(下面的链接 2),但没有发现与 HTML 部分的相关性。

我正在学习 Ruby(在 rails 之前)并且来自 C# .NetCF 背景(这里的 # 有点大声笑),并且确实可以帮助了解为什么 cmets 之间的符号不同和 代码 sn-ps

链接 1 Ruby - Difference between Array#<< and Array#push

链接 2 Why do some folks use Class#method instead of Class.method in correspondence?

【问题讨论】:

    标签: ruby


    【解决方案1】:

    简而言之,它只是一个 Ruby 文档约定。

    Foo#bar 用于类Foo实例方法 bar,您应该在代码中将其称为Foo.new.bar

    Foo::bar 用于类Foo类方法 bar,您应该在代码中将其称为Foo.bar

    【讨论】:

      【解决方案2】:

      这只是在编写 Ruby 代码时用于区分类/模块方法和实例方法的约定。

      Foo::bar 表示类/模块方法,Foo#bar 表示实例方法。

      请参阅this 问题及其答案,了解不同符号的历史。

      【讨论】:

        猜你喜欢
        • 2017-10-18
        • 1970-01-01
        • 2012-06-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-10-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多