I've created a module in the /lib folder:
Module CookieHelper
$str = cookies["shoppingcart"]
def get_all_cookie_info()
end
end
But this isn't working. If I move the code into a controller it works fine.
Also, I'm trying to invoke the method within this module. I've tried:
require CookieHelper
CookieHelper::get_all_cookie_info()
to use methods from a module inside of an other class (such as a controller), you do something like this:
include CookieHelper
Once you do that inside of your controller's class, you can call get_all_cookie_info() with just the method name.
It sounds like you might be trying to something odd, so if you want to spell out what get_all_cookie_info is supposed to do, then maybe I can offer more advice.
CookieHelper::get_all_cookie_info is the correct way to call this method.
include CookieHelper
get_all_cookie_info
is another valid way, if you want include all of the methods in cookie helper available without having to namespace them (once the file lib/cookie_helper has been loaded).
What the issue probably is, is that the lib file isn't even required yet, this is because rails3 doesn't automatically load files in lib anymore. You can tell it to do so by editing your application.rb file, and setting inside class Application < Rails::Application
config.autoload_paths += %W( #{config.root}/lib )
Related
I want to build an index for different objects in my Rails project and would like to add a 'count_occurences' method that I can call on String objects.
I saw I could do something like
class String
def self.count_occurences
do_something_here
end
end
What's the exact way to define this method, and where to put the code in my Rails project?
Thanks
You can define a new class in your application at lib/ext/string.rb and put this content in it:
class String
def to_magic
"magic"
end
end
To load this class, you will need to require it in your config/application.rb file or in an initializer. If you had many of these extensions, an initializer is better! The way to load it is simple:
require 'ext/string'
The to_magic method will then be available on instances of the String class inside your application / console, i.e.:
>> "not magic".to_magic
=> "magic"
No plugins necessary.
I know this is an old thread, but it doesn't seem as if the accepted solution works in Rails 4+ (at least not for me). Putting the extension rb file in to config/initializers worked.
Alternatively, you can add /lib to the Rails autoloader (in config/application.rb, in the Application class:
config.autoload_paths += %W(#{config.root}/lib)
require 'ext/string'
See this:
http://brettu.com/rails-ruby-tips-203-load-lib-files-in-rails-4/
When you want to extend some core class then you usually want to create a plugin (it is handy when need this code in another application). Here you can find a guide how to create a plugin http://guides.rubyonrails.org/plugins.html and point #3 show you how to extend String class: http://guides.rubyonrails.org/plugins.html#extending-core-classes
i have module Mymodule,inside lib directory, within few methods inside
module Mymodule
def usefull_meth(a,b)
a+b
end
end
i want to autoload it when my app started
i have inside my application.rb
config.autoload_paths += Dir["#{config.root}/lib/**/*"]
but i still need to include it like include Mymodule
i want to use my usefull_meth(a,b) inside application helper without inclusion
how i can achieve my goal? or i have done something wrong?
I simply want to have usefull_meth anywhere in my helpers,i do not need Mymodule.usefull_meth or smth else
but i still need to include it like include Mymodule
This is how Ruby works, it has nothing to do with Rails autoload.
In order to use some method globally, you should put it into global namespace. Just remove module definition and leave only method definition in that file(even though I don't understand why don't you put it into helper directly).
I achieved what i wanted
inside application.rb
require './lib/my_module'
include Mymodule
and now i have necessary methods with out include Mymodule
I have added options in application.rb:
config.autoload_paths += %W(#{config.root}/lib)
config.autoload_paths += Dir["#{config.root}/lib/**/"]
and lib\functions.rb:
def some_lib
return "#######################################"
end
In controller I'm trying to call this function, but get the error:
undefined local variable or method `some_lib' for #<TodosController:0x49a3850>
How can I fix it?
In order to have rails autoload from the lib dir, you need to follow rails naming conventions.
lib/functions.rb
class Functions
def self.some_lib
return "#######################################"
end
end
Then you can Functions.some_lib
Or
lib/functions.rb
module Functions
def some_lib
return "#######################################"
end
end
Then include Functions where you need your methods. This allows you to execute:
some_lib
Yeah, basically, don't do that, ruby is an OO language, you're trying to make a procedural language.
There's some way to make it do exactly what you ask involving mixing new methods into Kernel or Object... but that's really not what you want to do.
Do you want to add that new method to all controllers, and not neccesarily to other places? Then just add it to your ApplicationController (./app/controllers/application_controller.rb). Or add it to a module in ./lib, and then "include MyControllerFunctions" into ApplicationController.
Do you really want to be able to use it anywhere at all? Then I'd do what Kyle suggests, make it a module method, and call it as MyFunctions.some_method.
Ruby will let you do just about anything, you could manage to make it callable the way you want from any class at all... but really, you don't want to, it's just gonna lead to a mess.
I have a class that I want to re-open that is in a gem.
I put it in:
/lib/ClassName.rb
class ClassName
class << self
def some_method(a)
end
end
end
Now if I call this class method, it says its an undefined method.
I do have autoload set to the /lib folder.
Is this the wrong way to do this?
I find that sometimes I need to manually require certain files (especially one's that monkey patch existing classes/modules), even though the lib folder is being configured to autoload. I have yet to figure out why exactly.
To get around this, in config/initializers/application.rb (create it if necessary) I would require the file manually:
require 'ClassName'
I have a module called user_searches. It performs some searches that aren't core to the user model, thus, why I'm putting the responsibility somewhere else. I want to organize all my models like this that perform non-core user functions in a lib subfolder called user. Right now to include the module's methods in the User model I have to put...
require 'user/user_searches'
class User < ActiveRecord::Base
include UserSearches
end
...I don't need the require if the file is directly in the lib folder, but do if it's in the subfolder. What do I have to do so I don't need the require?
You could put the necessary require lines into lib/user.rb that way, all requirements are loaded recursively on application launch.
Alternatively, you could put something like this into an initializer:
# put into config/initializers/load_lib.rb
Dir["#{RAILS_ROOT}/lib/**/*.rb"].each { |f| require(f) }
It will require all ruby files in your lib folder. You just have to make sure if this is really what you want :)
This is works that cause
in file rails-2.2.2/lib/initializer.rb in method default_load_paths initialized to load path just the lib folder without subdirectories, to solve this you can edit your project ` environment.rb config file and push into config.load_path array all subdirs.