Question

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

NoMethodError: private method ‘set_element’ called for 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

Was it helpful?

Solution

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.

OTHER TIPS

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top