简体   繁体   English

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

[英]Why doesn't this extension method work in Rails?

In config/initializers: 在配置/初始化程序中:

Created file numeric_helper.rb: 创建的文件numeric_helper.rb:

  class Numeric
    def my_function
      self + 1
    end 
  end

rails console throws 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

Why does this throw an error? 为什么这会引发错误?

Working in Rails 3.07. 在Rails 3.07中工作。

EDIT: The rest of the error trace: 编辑:其余的错误跟踪:

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

There seems to be one or more of the following that fixed it: 似乎有以下一种或多种方法可以解决此问题:

  1. Restarting the machine 重启机器
  2. Windows line endings on the Ubuntu machine (editing on Windows, running Rails on Ubuntu). Windows在Ubuntu机器上的行尾(在Windows上编辑,在Ubuntu上运行Rails)。 Maybe that's why the error message was garbled. 也许这就是错误消息出现乱码的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM