5

调用set_elementMatrix 类的实例时,出现以下错误

NoMethodError: private method ‘set_element’ called for Matrix[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]:Matrix

set_element在文档中的公共实例方法下列出

矩阵#set_element

此外, set_element 是[]=(i, j, v)的别名,使用此方法出现以下错误

ArgumentError: wrong number of arguments (3 for 2)

没有任何意义,任何帮助表示赞赏。

红宝石 1.9.2 p180

4

2 回答 2

7

您可以简单地将 setter 函数公开,可能在您自己的类中(或在 Matrix 本身):

class SetableMatrix < Matrix
  public :"[]=", :set_element, :set_component
end
于 2012-01-24T15:19:01.613 回答
1

The documentation is incorrect. If you look at the matrix.rb file from 1.9.1, you'll see this:

def []=(i, j, v)
  @rows[i][j] = v
end
alias set_element []=
alias set_component []=
private :[]=, :set_element, :set_component

So the three methods are there but they are explicitly set as private.

A bit of quick experimentation indicates that a lot of the methods in the documentation are, in fact, private. There is a big block of documentation at the top of the man page that lists what are, apparently, supposed to be the available methods; that list doesn't match the list that rdoc has generated so there is some confusion.

I get the impression that instances of Matrix are meant to be immutable just like Fixnum and Number.

于 2011-06-24T05:43:50.820 回答