为什么这个扩展方法在Rails中不起作用?

时间:2012-03-13 01:06:03

标签: ruby-on-rails ruby ruby-on-rails-3 extension-methods

在config / initializers中:

创建文件numeric_helper.rb:

  class Numeric
    def my_function
      self + 1
    end 
  end

rails console抛出

/home/baller/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:235:in `load': /home/baller/rails/big_pimpin/config/initializers/numeric_helper.rb:1: syntax error, unexpected keyword_def, expecting '<' or ';' or '\n' (SyntaxError)
endss Nuend my_fself + 1

为什么会抛出错误?

使用Rails 3.07。

编辑:错误跟踪的其余部分:

from /home/baller/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:235:in 'block in load'
from /home/baller/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:225:in 'block in load_dependency'
from /home/baller/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:596:in 'new_constants_in'
from /home/baller/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:225:in 'load_dependency'
from /home/baller/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:235:in 'load'
from /home/baller/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.7/lib/rails/engine.rb:201:in 'block (2 levels) in <class:Engine>'
from /home/baller/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.7/lib/rails/engine.rb:200:in 'each'
from /home/baller/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.7/lib/rails/engine.rb:200:in 'block in <class:Engine>'
from /home/baller/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.7/lib/rails/initializable.rb:25:in 'instance_exec'
from /home/baller/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.7/lib/rails/initializable.rb:25:in 'run'
from /home/baller/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.7/lib/rails/initializable.rb:50:in 'block in run_initializers'
from /home/baller/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.7/lib/rails/initializable.rb:49:in 'each'
from /home/baller/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.7/lib/rails/initializable.rb:49:in 'run_initializers'
from /home/baller/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.7/lib/rails/application.rb:134:in 'initialize!'
from /home/baller/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.7/lib/rails/application.rb:77:in 'method_missing'
from /home/baller/rails/fann/config/environment.rb:5:in '<top (required)>'
from /home/baller/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.7/lib/rails/application.rb:103:in 'require'
from /home/baller/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.7/lib/rails/application.rb:103:in 'require_environment!'
from /home/baller/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.7/lib/rails/commands.rb:22:in '<top (required)>'
from script/rails:6:in 'require'
from script/rails:6:in '<main>'

1 个答案:

答案 0 :(得分:0)

似乎有以下一个或多个修复了它:

  1. 重新启动机器
  2. Ubuntu机器上的Windows行结尾(在Windows上编辑,在Ubuntu上运行Rails)。也许这就是为什么错误信息是乱码。