Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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

like image 714
Ram Avatar asked Jun 24 '11 05:06

Ram


Video Answer


2 Answers

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
like image 123
theldoria Avatar answered Dec 02 '22 18:12

theldoria


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.

like image 33
mu is too short Avatar answered Dec 02 '22 18:12

mu is too short