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.
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 need a place to stick global referentially transparent utility methods. These should be accessible from everywhere in rails (Models, Views, Controllers, and everywhere else). e.g.:
bool = GlobalUtilities.to_bool "false"
unicorn = GlobalUtilities.make_me_a "unicorn"
What's the best way to do this?
You could always stick these in /lib and require them. See bricker's answer -- you can require these modules to be loaded from your application.rb, which will make them accessible everywhere.
See: Rails lib directory
I would make a descriptively-named file for each behavior that your are hoping to achieve, and collect them in /lib (or, better yet, an engine). Then, mix your desired functionality into the class you hope to extend. For example, in the scenario you described, you could add a parse_boolean method directly to String. Pretty slick stuff.
/lib/add_parse_boolean_to_string.rb
class String
def parse_boolean
self == 'true' # or whatever...
end
end
I am using a plugin in Rails, and I call its methods without problems:
plugin_module::class_inside_module.method_a(...)
I want to re-open the class_inside_module and add a new method, I tried in many different ways. I can't figure out why in this way doesn't work:
class plugin_module::class_inside_module
def new_method
puts 'new method'
end
end
I get the error: uninitialized constant plugin_module, but how is possible if I can call without problem plugin_module::class_inside_module.any_methods ?
Do you know why I get that error ? why "uninitialized constant" ? (it is a class declaration :-O )
Do you have any ideas how I can add a new methods in a class inside a module (that is part of a plugin) ?
Thank you,
Alessandro
If you have written your class and module-names like you did, so plugin_module instead of PluginModule this is against ruby/rails standards, and rails will not be able to automatically find the class and module.
If you write something like
module MyModule
class MyClass
end
end
Rails will expect this file to be located in lib\my_module\my_class.
But this can always easily be overwritten by explicitly doing a require.
So in your case, when you write
module plugin_module::class_inside_module
Rails will not know where to find the module plugin_module.
This way of writing only works if module plugin_module is previously defined (and loaded).
So either add the correct require, or rename your modules to standard rails naming, or write it as follows:
module plugin_module
class class_inside_module
This way will also work, because now the order no longer matters.
If the module is not known yet, this will define the module as well.
Either you are re-opening the class, or you define it first (and the actual definition will actually reopen it).
Hope this helps.
Have you tried reopening the module that's wrapping the class, rather than relying on ::?
module plugin_module
class class_inside_module
def new_method
puts 'new_method'
end
end
end
By the way, you know that the proper name for modules and classes is use CamelCase with a capital first letter?
module PluginModule
class ClassInsideModule
def new_method
puts 'new_method'
end
end
end
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 )