Use Ruby Class In A Different Directory - ruby

I have written a few ruby classes. However, when trying to access one from another directory I am getting the following error:
uninitialized constant Main::AppVersion
This is what the directory structure looks like:
home --> a --> app_version.rb
home --> b --> c --> lib --> main.rb (and other classes)
Everything within "lib" can see each other. However, when trying to access app_version, it fails. I added the path to app version (home/a) to the $LOAD_PATH. So it should be available from there. I have also tried "requiring" my other class, but when I do that I get the following error:
LoadError: no such file to load -- AppVersion
Any idea on what I could be doing wrong here would be highly appreciated. Thanks!

Can try using require_relative:
require_relative '../../../a/app_version'

You error is:
LoadError: no such file to load -- AppVersion
So require is looking for a file AppVersion.rb. And you said the file name is app_version.rb. Try to load it with:
require 'app_version'
after setting up the $LOAD_PATH.

When you define Main::AppVersion class it's supposed to reside in main folder (i.e. main/app_version.rb)
So it doesn't depend on $LOAD_PATH.

You will have to require the file.
LoadError: no such file to load -- AppVersion
Do you require "app_version" ? Or require "AppVersion"? (first one is correct, and consider to require_relative)
Another option is to run ruby and give the include path like
ruby -Ia -Ib/c/lib b/c/lib/main.rb
.

Related

Unable to include file in the spec file

I am trying to write an RSpec test for a ruby project(Sketchup plugin) but I am facing an issue with the require.
Below is how my folder is structured
In my smoke_tests_spec.rb
require "main_folder/subfolder1/file_to_test.rb"
When I run the rspec using rspec-core/rspec from the root directory I get the following error
Failure/Error: require "main_folder/subfolder1/file_to_test.rb"
LoadError:
cannot load such file -- main_folder/subfolder1/file_to_test.rb
You need to change the LOAD_PATH or use require_relative
Here you have more info.

Why I am getting LoadError for require 'support/number_helper' in Ruby

When I am using the require_relative 'support/number_helper' it's working fine, But when I am using require 'support/number_helper' then I am getting this error.
rubies/ruby-2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- ./support/number_helper (LoadError)
I also tried this but getting the same error.
require './support/number_helper
I am using the ubuntu.
require_relative 'support/number_helper' searches for a file to load by adding given string to the directory of current_file (__FILE__). For example, in your project folder you have 2 files:
lib/special_gem/fetcher.rb
lib/special_gem/support/number_helper.rb
You can use require_relative 'support/number_helper' in your 1st file to load the 2nd. The command takes the path to the directory of the current file (lib/special_gem/), appends given string (support/number_helper) and successfully finds file to load.
What about require command, if given path is not absolute, it will search for the file in the directories listed in $LOAD_PATH. Very likely your lib folder is in this list, so to load 2nd file you could use the command
require 'special_gem/support/number_helper'
Since it's not relative, you can use it from your 1st file or any other file of your project.
When using require 'support/number_helper' it will search for the file at lib/support/number_helper. If that file is missing, LoadError exception is raised.
See the documentation for details.

Ruby including files

I have a Ruby app that runs on a server with no web interface. It is run using the command line(ruby path/to/file.rb).
I have classes in different files that I want to be accessible. The files are located in the "app/classes" directory.
I put this in the application.rb file:
config.autoload_paths += Dir["#{config.root}/classes"]
and I get an uninitialized constant error.
I can put in "require_relitive 'somefile'" but I would rather not have to do this for every class that is used. How do I create an autoload path and where should it be located at?
Use require_all
See https://github.com/jarmo/require_all
It basically allows you to write this:
require 'require_all'
require_all 'app/classes'
And all ruby files in app/classes will be loaded.

Require path errors thrown

I wrote a ruby class that I want to use in other applications that I have. I put it in the same directory c:\apps that I have my other applications in. When I require my class it says that:
`require': no such file to load --
Even though the file is in the same directory as the application I am running. I am simply doing a :
require 'fileformat'
ok, so I did /apps/fileformat and it worked

How do I require a file from inside a directory with Ruby?

I think I'm missing something here. I have a directory like this:
myapp
|-lib
|-package1
|-dostuff.rb
|-package2
|-dostuff.rb
From an irb console I'm trying to test the library before I add it to my real project (a Rails app). However, typing this:
require 'lib/package1/dostuff'
returns an error saying it can't find the file to load. I added the lib directory to the load path but I'm not able to load the file.
What am I forgetting? The two filenames don't have to be the same but that's how they are to begin with (they are auto-generated from some web services I need to call using soap4r; each package represents a different web service API group)
If the directory "lib" is in the load path, the argument to require must be relative to lib. So require 'package1/dostuff' without the lib, otherwise it will look for lib/lib/package1/dostuff.rb.
In Ruby 1.9 there's the new require_relative method, which would let you do require_relative "../package2/dostuff" from within package1/dostuff.rb.

Resources