2012-02-04 53 views
1

我曾在使用擴展類中軌,特別是擴展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

一般的答案已在軌已經出現自動加載3. 然而,當我伸出 '數學'新功能的工作,當我延長'矩陣'的新功能不起作用 - 即使我以相同的方式對待他們

我已經嘗試了很多迭代和更改模塊名稱,要求自動加載,但這裏是我的最新密鑰文件:

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

難道是因爲數學是在Ruby模塊,但矩陣是一類?如果是這樣,我該如何糾正?

回答

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

+0

謝謝!這一直困擾着我一個星期,你的解決方案是完美的。我希望我能給你更多的觀點。 – Zakoff 2012-02-04 10:48:04

+0

非常感謝您的反饋,僅此一項就值得您想要額外提供的任何觀點;-) – mliebelt 2012-02-04 11:02:04

相關問題