Note that there are some explanatory texts on larger screens.

plurals
  1. POAm I extending this inbuilt ruby class correctly?
    primarykey
    data
    text
    <p>In my rails app in lib/matrix.rb I have entered the following code to extend the inbuilt Matrix class:</p> <pre><code>module Matrix require 'matrix' class Matrix def symmetric? return false if not square? (0 ... row_size).each do |i| (0 .. i).each do |j| return false if self[i,j] != self[j,i] end end true end def cholesky_factor raise ArgumentError, "must provide symmetric matrix" unless symmetric? l = Array.new(row_size) {Array.new(row_size, 0)} (0 ... row_size).each do |k| (0 ... row_size).each do |i| if i == k sum = (0 .. k-1).inject(0.0) {|sum, j| sum + l[k][j] ** 2} val = Math.sqrt(self[k,k] - sum) l[k][k] = val elsif i &gt; k sum = (0 .. k-1).inject(0.0) {|sum, j| sum + l[i][j] * l[k][j]} val = (self[k,i] - sum) / l[k][k] l[i][k] = val end end end Matrix[*l] end end end </code></pre> <p>Is this the correct way to add methods to an existing class within the rails app? Should I have the <strong>require matrix</strong> line there?</p> <p>EDIT 1: Additional info provided</p> <p>I have now removed the require 'matrix' line.</p> <p>If I type the following test code in a view page, <strong>it only works if I delete my lib/matrix.rb file</strong>:</p> <pre><code>&lt;% require 'matrix' %&gt; &lt;% m = Matrix[ [0,0], [1,1] ] %&gt; &lt;%= m.column(0) %&gt; </code></pre> <p>Otherwise it gives the error:</p> <pre><code>undefined method `[]' for Matrix:Module </code></pre> <p>It appears that I am eliminating the default methods of the built in Matrix class when I try to extend the class. Is there a way around this error? </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload