两个扩展类 - 一个工作,另一个不工作

时间:2012-02-04 10:21:12

标签: ruby-on-rails-3 class

我在rails中使用扩展类时遇到了一些困难,特别是扩展了Matrix类 - 我还问了一些与此相关的问题:

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

一般来说,答案一直是在rails 3中进行自动加载。但是,当我扩展'Math'新功能时,当我扩展'Matrix'时,新功能不起作用 - 即使我在同样的方式

我尝试过多次迭代并更改模块名称,需要自动加载,但这是我最新的密钥文件:

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

我的观看页面:

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

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

可能是因为Math是ruby中的模块,但Matrix是一个类吗?如果是这样,我该如何纠正?

1 个答案:

答案 0 :(得分:1)

如果你看一下Matrix的实现,你会看到原因。对我来说,它位于.../ruby192/lib/ruby/1.9.1/matrix.rb。定义是(省略所有方法):

class Matrix
  include Enumerable
  include ExceptionForMatrix
  ...
end

这意味着Matrix未包含在模块中。您的补充来源应该开始:

class Matrix

  def symmetric?
    ...
  end
  def cholesky_factor
    ...
  end
end

因此,您对类或模块的添加必须与当前定义匹配。 Matrix在Ruby常量中称为Matrix,而不是Extend_matrix::Matrix,这是您定义的。