Ruby Class#new - 为什么“new”是私有方法?

发布于 2024-10-25 16:42:40 字数 464 浏览 3 评论 0原文

我创建了一个 Matrix 类,我想在代码的各个部分使用它。

class Matrix
  def initialize(x, y, v=0)
    @matrix = Array.new
    (0..y).each do |j|
      @matrix[j] = Array.new
      (0..x).each do |i|
        @matrix[j][i] = v
      end
    end
  end
end

当此代码与使用它的代码包含在同一类中时,一切都会正常运行。

当我将此代码移至 lib/matrix.rb 并需要它时,出现以下错误:

./phylograph:30:in `block in run': private method `new' called for Matrix:Class (NoMethodError)

I made a Matrix class and I want to use it in various parts of my code.

class Matrix
  def initialize(x, y, v=0)
    @matrix = Array.new
    (0..y).each do |j|
      @matrix[j] = Array.new
      (0..x).each do |i|
        @matrix[j][i] = v
      end
    end
  end
end

When this code is included in the same class as the code that uses it, everything runs fine.

When I move this code to lib/matrix.rb and require it, I get the following error:

./phylograph:30:in `block in run': private method `new' called for Matrix:Class (NoMethodError)

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

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

发布评论

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

评论(3

往事随风而去 2024-11-01 16:42:40

我记得,Matrix 是一个纯粹的函数类;它的对象是不可变的,并且简单地创建一个新的 Matrix 对象通常是无用的,因为 API 没有任何可变操作。

因此,新的 Matrix 对象是由 API 创建的,该 API 在用户级别不使用 new 。

这是作者做出的设计决定。

更新:OIC,您无意使用标准库 Matrix 类。因此,从技术上讲,上述是您问题的原因,但对我来说,直接说:

您对 Matrix 的定义与 Ruby 标准发生冲突
同名的库类。

As I recall, Matrix is a purely functional class; its objects are immutable, and simply creating a new Matrix object is normally useless as the API doesn't have any mutable operations.

So, new Matrix objects are created by an API that just doesn't use new at the user level.

It's a design decision made by the author.

Update: OIC, you had no intention of using the standard library Matrix class. So the above is technically the reason for your problem but it would have been more helpful for me to just say:

Your definition of Matrix is clashing with the Ruby Standard
Library class of the same name.

零度° 2024-11-01 16:42:40

这是因为 Matrix 是标准 ruby​​ 库中的一个类,请尝试给出您的类有不同的名称或将其放入模块中。

It's because Matrix is a class from the standard ruby library, try giving your class a different name or put it inside a module.

骷髅 2024-11-01 16:42:40

至于为什么只有当您将其移动到 lib/matrix.rb 时该错误才会咬您:

在移动它之前,您的代码中没有 require 'matrix' ,所以你没有加载矩阵标准库。但是,当您移动它并在代码中添加 require 'matrix' 时,您就加载了矩阵标准库。

这就是为什么在编写库时,建议您仅使一个文件对其他文件可见代码。想象一下,如果矩阵库有其他代码可见的其他文件,问题会有多严重!

As for why the bug only bit you when you moved it to lib/matrix.rb:

Before you moved it, you didn't have require 'matrix' in your code, so you didn't load the matrix standard library. But when you moved it, and added require 'matrix' in your code, you loaded the matrix standard library.

This is why, when writing libraries, you're advised to only make one file visible to other code. Imagine how much worse the problem would be if the matrix library had other files visible to other code!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文