在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>'
答案 0 :(得分:0)
似乎有以下一个或多个修复了它: