Rails: requiring a library in a controller - ruby-on-rails

The following way works perfectly
require "#{Rails.root}/lib/mymodule/class1.rb"
But why do do I have to put #{Rails.root}

As of Ruby 2.0, Ruby no longer considers the current directory to be in the require path. You could also use require ./lib/mymodule/class1.rb.

Related

How to import without specifing path in ruby

When I develop ruby app.
I found that some apps import other modules without specifying specific path like following
require 'test'
I always set some modules in certain directory and set following path.
require './sample/test'
How can I import without path ?
Are there any environment setting around this?
Am I missing important thing?
If someone has opinion,please let me know.
Thanks
As was mentioned in the comments, Ruby looks at the $LOAD_PATH global variable to know where to look for required libraries.
Normally, under most circumstances, you should not mess with it and just leave it as is.
When you see other libraries using require without a leading dot for relative path (e.g., require 'sinatra'), it usually means one of these:
They load a pre-installed gem, and since the gem home path is part of the $LOAD_PATH, it can be found and loaded.
The code you see is a part of a gem, and it loads one of its own files.
The code you see is a part of a larger framework (e.g., Rails), which has altered the $LOAD_PATH variable.
You can see all the folders available in the $LOAD_PATH like this:
pp $LOAD_PATH
If, for some reason, you insist on altering the $LOAD_PATH to load your own local files, you can do so like this:
$LOAD_PATH.unshift __dir__
require 'my-local-file'
Bottom line, my recommendation to you is: Do not use this technique just so that you require statements "look nicer", and if you have functionality that can be encapsulated in a gem (private or public), do it.

Rails requiring standard Ruby libraries?

I ran into an issue testing a new app in production environment.that Net::HTTP was not defined, but in development it was.
Naturally a require 'net/http' somewhere solves this (e.g. I put it in config/application.rb after Bundler.require(*Rails.groups),as I understand, there is no gem name I can add to Gemfile).
But can I find a list of which Ruby standard modules/classes need to be required, or should I just start adding everything I need to application.rb to be clear (date, json, net/http, etc.)?

Require a gem in rails

I'd like to include unitwise in my project, so I added it in the Gemfile and I want to use the core extensions of this gem in a model, so I have to require 'unitwise/ext' which isn't by default. Should I require this file in every models I use it, or is there a way to require it one time for the whole project?
You can require it once either by creating an initializer for it, or adding this line to application.rb.
require 'unitwise/ext'
You could create separate ruby file in config/initializer/ to require unitwise/ext which will be available in all place in the project or you could require in application.rb, here also it will be available everywhere in application.
It is better add one line in application.rb, instead creating separate file in initializer to require that file.

require 'lib/my_module' in deploy.rb

I have a module in in lib/redmine.rb that has some classes and stuff. I can call Redmine.some_method from the console just fine, but I'd like this module to be loaded during deployment using Capistrano.
I've tried:
require 'lib/redmine'
require 'redmine'
require './lib/redmine'
require '../lib/redmine'
load 'lib/redmine'
and all of those with a .rb at the end of it.
I can't seem to get access to the Redmine module from the deploy namespace...
The best way would be if you turn it into a gem project, and then simply refer to this
project via the name you gave it.
The problem I have with the code above is that I do not know the layout you use inside of redmine.rb and you are not showing the specific error.
Note that require() tries to go to the ruby SITE_DIR path first, so it would expect require 'redmine' to be installed like a gem (or, via setup.rb which was the old way before gem was written).
Try this too for ad-hoc solution (but don't use it in production code; you modify the load path and this is not good nor needed; use the gem project layout and installation, it is much easier and safer in the long run):
$: << '.'
require './redmine.rb'

ruby gem, requiring a rails module bad practice?

I am working on a ruby gem that parses xml. I want to utilize the rails Hash.from_xml method and am wondering if requiring active_support or any large library in a gem is bad practice. Is this adding too much just so I can use its one method, or is this considered standard/ok when building a ruby gem? I would be adding require 'active_support/all' to my gem.
I prefer to require as little as possible and to only cherry-pick the specific definition that I want to use.
In your example that would look like this:
require 'active_support'
require 'active_support/core_ext/hash/conversions'
Read about how to require only specific definitions from ActiveSupport in the official Rails Guides.

Resources