开发者

Ruby Matrix set_element private?

When calling set_element on an instance of the Matrix class I get the following error

NoMethodError: private method ‘set_element’ called fo开发者_开发百科r Matrix[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]:Matrix

But set_element is listed under public instance methods in the documentation

Matrix#set_element

Also, set_element is an alias for []=(i, j, v) and using this method I get the following error

ArgumentError: wrong number of arguments (3 for 2)

Doesn't make any sense, any help is appreciated.

Ruby 1.9.2 p180


You can simply make the setter functions public, possibly in your own class (or at Matrix itself):

class SetableMatrix < Matrix
  public :"[]=", :set_element, :set_component
end


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜