Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7400621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:16:34+00:00 2026-05-29T04:16:34+00:00

I have had some difficulties in using extended classes in rails, in particular extending

  • 0

I have had some difficulties in using extended classes in rails, in particular extending the Matrix class – and I have also asked some questions related to this:

Am I extending this inbuilt ruby class correctly

Using custom functions in rails app

Where do I put this matrix class in my rails app

In general the answers have been around autoloading in rails 3. However, when I extend ‘Math’ new functions work, when I extend ‘Matrix’ new functions don’t work – even if I treat them in the same way

I’ve tried many iterations and change module names, requires, autoloads but here are my latest key files:

application.rb:

require File.expand_path('../boot', __FILE__)

require 'rails/all'
require 'matrix'

# If you have a Gemfile, require the gems listed there, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env) if defined?(Bundler)

module SimpleFixed
  class Application < Rails::Application
    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.

    # Custom directories with classes and modules you want to be autoloadable.
    # **I have tried all these autoload variants**
    #  config.autoload_paths += %W(#{config.root}/extras)
    #  config.autoload_paths += %W(#{config.root}/lib)
    #  config.autoload_paths += Dir["#{config.root}/lib/**/"]
    #  config.autoload_paths << "#{Rails.root}/lib"
      config.autoload_paths << "#{::Rails.root.to_s}/lib" # <-- set path
      require "extend_matrix" # <-- forcibly load your matrix extension

*other standard code here*

lib/extend_math.rb

module Math
  class << self

    def cube_it(num)
      num**3
    end

    def add_five(num)
      num+5
    end

  end
end

lib/extend_matrix.rb

module Extend_matrix **An error is raised if I call this 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 > 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

my view page:

<%= Math.add_five(6) %> **works*

<%= Matrix[[25,15,-5],[15,18,0],[-5,0,11]].cholesky_factor %> **doesn't work**

Could it be because Math is a Module in ruby but Matrix is a class? If so, how do I correct for this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T04:16:34+00:00Added an answer on May 29, 2026 at 4:16 am

    If you have a look at the implementation of Matrix, you will see the reason. For me, it is located in .../ruby192/lib/ruby/1.9.1/matrix.rb. The definition is (omitted all methods):

    class Matrix
      include Enumerable
      include ExceptionForMatrix
      ...
    end
    

    That means that Matrix is not contained in a module. Your source for your additions should begin:

    class Matrix
    
      def symmetric?
        ...
      end
      def cholesky_factor
        ...
      end
    end
    

    So your addition to a class or module has to match the current definition. Matrix is known as Matrix in the Ruby constants, and not as Extend_matrix::Matrix which is what you have defined.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that people have had some trouble with undefined CssClass values when using
I am successfully databinding and using WPF Comboboxes and have had some success with
In my limited experience, I've been on several projects that have had some sort
Here is a question that I have had for some time but never actually
What are some strategies that people have had success with for maintaining a change
I have had a hard disk failure which resulted in some files of a
I have a live database that had some data deleted from it and I
I would love to get some tips from other people that have had this
I'm sure that all of us have had to deal with telecommuters at some
I have a little problem. I had some JComboBox to a JDialog but they

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.