Rails Download Questions

Let's say I have some custom classes that are not related to models, controllers, etc., should I put this in / lib correctly?

On rails <3 I would add this directory to my loadpath, and on rails 3+ I would add this to my autoload_path. Is it correct?

Now say that I have some classes that extend already defined classes. Where should I put this so that it starts at startup. For example, I want to add the 'foo' method to a String.

class String
  def foo
    'foo;
  end
end

Where should I put this class so that it is defined at startup?

Another weird mistake I come across is when I try to use namespace classes in lib.

module MyProject
 class foo
 end
end

Now in the console:

ruby-1.9.2-p136 :004 > MyProject::Foo
LoadError: Expected /Users/me/workspace/my_project/lib/foo.rb to define Foo
 from /Users/rob/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:492:in `load_missing_constant'
 from /Users/rob/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:183:in `block in const_missing'

. ?

+3
2

3 config/application.rb

#config.autoload_paths += %W(#{config.root}/extras)

, lib.

+8

, , , / load_path Rails 2 autoload_path Rails 3.

, , , config/initializers.

, , , Foo foo.rb , (Bags MyProject).

, , , autoload_paths , . "autoload" "require" Ruby.

+3

Source: https://habr.com/ru/post/1784125/


All Articles