For example I would like to create a current_server_host method that returns the current host the application is currently running from.
lib/utility.rb:
module Utility
def current_server_host
request.env['HTTP_HOST']
end
end
How do I make it available project-wide where I don’t need to include anything but can just call via Utility.current_server_host?
How do I make it specific to the class (model/controller/view) where I do need to call include?
Please answer for RoR 3.2+.
Thanks!
To make Utility methods available as class methods in another class, use extend:
A ruby module can extend itself in the same manner in which it can extend classes:
If you want to provide Utility methods to instances of a class, use include: