require bindata in ruby on rails application - ruby-on-rails

I have a set of files that are in the lib directory of a Ruby on Rails application.
I have a model that needs to use these files. In my model I have the following:
require_relative '../../some_path_to_file_without_extention'
(Side note; I would love to know a way to require all the files, instead of require_relative for each file).
The file that I require_relative has the following require in it.
require "bindata"
When I try to access functions from the require_relative file I get the following error:
LoadError: cannot load such file -- bindata
This is happening for other gems that are being required in the set of files as well. I just chose bindata as an example.
I have bindata in my Gemfile. When I run bundle show bindata it shows me the path to bindata.
I even put require 'bindata' in my model, but it gave me the same load error.
How do I stop the LoadError?
Any help would greatly be appreciated.
Update 1
When I run bundle show . I get the following:
Gems included by the bundle:
...
* bcrypt (3.1.11)
* bindata (2.3.4)
...
Then in the console, requiring bcrypt works but bindata does not.
irb(main):002:0> require 'bcrypt'
=> true
But bindata does not.
irb(main):003:0> require 'bindata'
LoadError: cannot load such file -- bindata
Update 2
Ok so I know is has to be something with how I am loading my rails environment.
bundle exec irb
irb(main):001:0> require 'bindata'
=> true
Update 3
So I went back a few git commits and keep trying to add the gems and see if they would load in my rails console. I went back far enough were it did. Did not know what was different. However, I also noticed when my spring server was restarted then my gems would load in my rails console.

To require all the files in the lib folder, add config.autoload_paths << Rails.root.join('lib') to your application.rb, this answer can help you: https://stackoverflow.com/a/19650564/3739191
Now,
require 'bindata'
should work :)

Related

jruby no such file to load application.rb

I have a project in jRuby that I try to run.
So I'm installling jruby using rvm like this
rvm install jruby-9.1.15.0 (bc thats the version required by the project)
then im making sure that im actually using jruby by typing
ruby -v which shows me that I have jruby
next thing, I want to run the rails server so I'm typing
bundle exec rails server
and unfortunately I get an error:
/Users/foouser/Documents/Projects/fooproject/bin/config/application
LoadError: no such file to load -- /Users/foouser/Documents/Projects/fooproject/bin/config/application
require at org/jruby/RubyKernel.java:955
block in server at /Users/foouser/.rvm/gems/jruby-9.1.15.0-jrubu/gems/railties-4.2.10/lib/rails/commands/commands_tasks.rb:78
tap at org/jruby/RubyKernel.java:1741
server at /Users/foouser/.rvm/gems/jruby-9.1.15.0-jrubu/gems/railties-4.2.10/lib/rails/commands/commands_tasks.rb:75
run_command! at /Users/foouser/.rvm/gems/jruby-9.1.15.0-jrubu/gems/railties-4.2.10/lib/rails/commands/commands_tasks.rb:39
<main> at /Users/foouser/.rvm/gems/jruby-9.1.15.0-jrubu/gems/railties-4.2.10/lib/rails/commands.rb:17
require at org/jruby/RubyKernel.java:955
<main> at bin/rails:5
I've tried googling that but to no avail. People seem to not encounter this issue at all. I'm sure it's got something to do with my setup because other people are successfully using said project.
Any ideas?
rails version - 4.2
jruby version - 9.1.15.0
EDIT:
after some digging, in bin/rails file the line defining the APP_PATH was like that:
APP_PATH = File.expand_path('../config/application', __FILE__)
which somehow made it looking for application.rb inside fooproject/bin/config instead of fooproject/config
but when I changed the line to
APP_PATH = File.expand_path('../../config/application', __FILE__)
it goes to the correct path where application.rb exist but still reports it as not existing.
the error is nearly the same (apart for path)
LoadError: no such file to load -- /Users/foouser/Documents/Projects/fooproject/config/application
require at org/jruby/RubyKernel.java:955
block in server at /Users/foouser/.rvm/gems/jruby-9.1.15.0-jrubu/gems/railties-4.2.10/lib/rails/commands/commands_tasks.rb:78
tap at org/jruby/RubyKernel.java:1741
server at /Users/foouser/.rvm/gems/jruby-9.1.15.0-jrubu/gems/railties-4.2.10/lib/rails/commands/commands_tasks.rb:75
run_command! at /Users/foouser/.rvm/gems/jruby-9.1.15.0-jrubu/gems/railties-4.2.10/lib/rails/commands/commands_tasks.rb:39
<main> at /Users/foouser/.rvm/gems/jruby-9.1.15.0-jrubu/gems/railties-4.2.10/lib/rails/commands.rb:17
require at org/jruby/RubyKernel.java:955
<main> at bin/rails:4
EDIT 2:
requested permissions for application.rb:
-rw-r--r-- 1 foouser staff 5818 20 Jun 15:12 config/application.rb
config.ru:
# This file is used by Rack-based servers to start the application.
require ::File.expand_path('../config/environment', __FILE__)
run Rails.application
environment.rb
# Load the Rails application.
require File.expand_path('../application', __FILE__)
# Initialize the Rails application.
Rails.application.initialize!
So, the solution I have is the "have you tried to turn it off and on again?" type.
I have deleted the whole project and cloned it again from the repo.
Everything is working now, it must have been something with my local env but I can't tell what.
Anyway, if you ever encounter something similar - purge and clone is definitely something you can try.

Load gems inside scripts run from the shell outside our Rails application directory?

We have a mail gem installed in our vendor/cache directory inside a Rails application.
The script is called "test" and is not inside the Rails application directory.
#! /usr/local/rvm/wrappers/ruby-1.9.3-p194/ruby
require 'date'
require 'fileutils'
require 'openssl'
require 'yaml'
require 'mail'
require 'dalli'
I get the following error when I execute this script from outside the Rails application.
/usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- mail (LoadError)
from /usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
I simply re-installed these gems (mail, dalli) in the standard Ruby path and it worked, but that's not ideal.
What do we need to so that these installed gems are found when we try to run this script outside of a Rails app? In other words, how do we specify the path to these gems?
Be sure that your gem are all declared in your Gemfile:
gem 'mail'
If you don't want them to be loaded by default, and load them only when needed, you can use the require statement that you are already using, and in your Gemfile add :require => false:
gem 'mail', :require => false
When you call your script from outside your Rails environment, and want to load the gems, prefix your script by bundle exec:
bundle exec my_script.rb
If you need to run your script from another location than your rails's app root, you must run:
BUNDLE_GEMFILE=/path/to/your/app/Gemfile bundle exec your_script
Keep in mind though that this may cause path issues if your script or your gems are looking for file in the path of your rails app

require 'watir-webdriver' error

I am trying to use Watir with Rails, and installed it, and when I use it from irb, it works fine. But when I try to include it in rails application, as
require 'rubygems'
require 'watir-webdriver'
I get the following error:
LoadError (no such file to load -- watir-webdriver)
Some people had experienced the same problem, which was solved by using require 'rubygems', butin my case, the problem still persists. Any idea?
Did you add gem 'watir-webdriver' to your Gemfile in Rails root folder?

Rails tests can't find test_helper

I'm trying to run individual tests through ruby test/unit/mytest.rb, but I always get a "no such file to load - test_helper" error. Google brought up a few suggestions, but none of them worked for me. I'm running Rails 3.0, Ruby 1.9.2 (through RVM) on Ubuntu 10.10
Here's what I've tried so far - any suggestions really appreciated
Changed the "require test_helper" to "require File.dirname(FILE) + "/../test_helper"
" in test/unit/mytest_test.rb. It brings back " no such file to load -- test/unit/../test_helper"
Tried running rvm test/unit/mytest_test.rb Same as above
Tried running ruby -I test/unit/mytest_test.rb. No messages to the terminal. After about 5 minutes waiting for something to happen, ctrl+c'd out of it
Any suggestions very appreciated - I'm stumped.
ruby 1.9.2 removed ".", the current directory, from the load path. I have to do this to get it to work:
require 'test_helper'
and call it like:
ruby -I. unit/person_test.rb
I was fighting this thing myself today and i dislike the big require with whole path to file and stuff...
In my case it was fault of Rakefile..
so now it looks like this:
require "bundler/gem_tasks"
require "rake/testtask"
Rake::TestTask.new do |t|
t.libs << "lib"
t.libs << "test" # here is the test_helper
t.pattern = "test/**/*_test.rb"
end
task default: :test
I know its old and has answer marked accepted, but maybe this will also help someone :)
have a nice day
I've added the following to the top of my test files.
require File.expand_path("../../test_helper", __FILE__)
This restores the previous behavior and allows the call to be simply:
ruby test/unit/person_test.rb
Maybe you should run your test cases in this way:
$ rake test
There is no need to change the "require" statement from generated code if you use rake.
Tested with Ruby 1.9.3 and Rails 3.2.8
If you are creating a gem or engine, running rake test in the test dummy application directory will cause this error. Running rake test in the root of the gem will avoid this.
Rails 1.9 no longer includes the current directory in the LOAD_PATH, which causes this problem. You have a few options.
call the test with the -I option from the app dir:
ruby -I test test/functional/test_foo.rb
and use a require with no path:
require "test_helper.rb"
use a full path in the require.  Either
require 'pathname'
require Pathname.new(FILE).realpath.dirname.join('/../test_helper.rb')
or
require (File.dirname(File.realdirpath(__FILE__)) + '/../test_helper.rb')

How does load differ from require in Ruby?

Is there any major difference between load and require in the Ruby on Rails applications? Or do they both have the same functionality?
require searches for the library in all the defined search paths and also appends
.rb or .so to the file name you enter. It also makes sure that a library is only
included once. So if your application requires library A and B and library B requries library A too A would be loaded only once.
With load you need to add the full name of the library and it gets loaded every time you
call load - even if it already is in memory.
Another difference between Kernel#require and Kernel#load is that Kernel#load takes an optional second argument that allows you to wrap the loaded code into an anonymous empty module.
Unfortunately, it's not very useful. First, it's easy for the loaded code to break out of the module, by just accessing the global namespace, i.e. they still can monkeypatch something like class ::String; def foo; end end. And second, load doesn't return the module it wraps the code into, so you basically have to fish it out of ObjectSpace::each_object(Module) by hand.
I was running a Rails application and in Gemfile, I had a specific custom gem I created with the option "require: false". Now when I loaded up rails server or rails console, I was able to require the gem in the initializer and the gem was loaded. However, when I ran a spec feature test with rspec and capybara, I got a load error. And I was completely bewildered why the Gem was not found in $LOAD_PATH when running a test.
So I reviewed all the different ways that load, require, rubygems and bundler interact. And these are a summary of my findings that helped me discover the solution to my particular problem:
load
1) You can pass it an absolute path to a ruby file and it will execute the code in that file.
load('/Users/myuser/foo.rb')
2) You can pass a relative path to load. If you are in same directory as file, it will find it:
> load('./foo.rb')
foo.rb loaded!
=> true
But if you try to load a file from different directory with load(), it will not find it with a relative path based on current working directory (e.g. ./):
> load('./foo.rb')
LoadError: cannot load such file -- foo.rb
3) As shown above, load always returns true (if the file could not be loaded it raises a LoadError).
4) Global variables, classes, constants and methods are all imported, but not local variables.
5) Calling load twice on the same file will execute the code in that file twice. If the specified file defines a constant, it will define that constant twice, which produces a warning.
6) $LOAD_PATH is an array of absolute paths. If you pass load just a file name, it will loop through $LOAD_PATH and search for the file in each directory.
> $LOAD_PATH.push("/Users/myuser")
> load('foo.rb')
foo.rb loaded!
=> true
require
1) Calling require on the same file twice will only execute it once. It’s also smart enough not to load the same file twice if you refer to it once with a relative path and once with an absolute path.
2) require returns true if the file was executed and false if it wasn’t.
3) require keeps track of which files have been loaded already in the global variable $LOADED_FEATURES.
4) You don’t need to include the file extension:
require 'foo'
5) require will look for foo.rb, but also dynamic library files, like foo.so, foo.o, or foo.dll. This is how you can call C code from ruby.
6) require does not check the current directory, since the current directory is by default not in $LOAD_PATH.
7) require_relative takes a path relative to the current file, not the working directory of the process.
Rubygems
1) Rubygems is a package manager designed to easily manage the installation of Ruby libraries called gems.
2) It packages its content as a zip file containing a bunch of ruby files and/or dynamic library files that can be imported by your code, along with some metadata.
3) Rubygems replaces the default require method with its own version. That version will look through your installed gems in addition to the directories in $LOAD_PATH. If Rubygems finds the file in your gems, it will add that gem to your $LOAD_PATH.
4) The gem install command figures out all of the dependencies of a gem and installs them. In fact, it installs all of a gem’s dependencies before it installs the gem itself.
Bundler
1) Bundler lets you specify all the gems your project needs, and optionally what versions of those gems. Then the bundle command installs all those gems and their dependencies.
2) You specify which gems you need in a file called Gemfile.
3) The bundle command also installs all the gems listed in Gemfile.lock at the specific versions listed.
4) Putting bundle exec before a command, e.g. bundle exec rspec, ensures that require will load the version of a gem specified in your Gemfile.lock.
Rails and Bundler
1) In config/boot.rb, require 'bundler/setup' is run. Bundler makes sure that Ruby can find all of the gems in the Gemfile (and all of their dependencies). require 'bundler/setup' will automatically discover your Gemfile, and make all of the gems in your Gemfile available to Ruby (in technical terms, it puts the gems “on the load path”). You can think of it as an adding some extra powers to require 'rubygems'.
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
2) Now that your code is available to Ruby, you can require the gems that you need. For instance, you can require 'sinatra'. If you have a lot of dependencies, you might want to say “require all of the gems in my Gemfile”. To do this, put the following code immediately following require 'bundler/setup':
Bundler.require(:default)
3) By default, calling Bundler.require will require each gem in your Gemfile. If the line in the Gemfile says gem 'foo', :require => false then it will make sure foo is installed, but it won’t call require. You’ll have to call require('foo') if you want to use the gem.
So given this breadth of knowledge, I returned to the issue of my test and realized I had to explicitly require the gem in rails_helper.rb, since Bundler.setup added it to $LOAD_PATH but require: false precluded Bundler.require from requiring it explicitly. And then the issue was resolved.

Resources