Ruby error : Cannot load such file - ruby

I am ruby beginner. Two file is here first file attached to second file by using require. But first file does not load.
first.rb
puts "First File"
second.rb
require 'first'
puts "Second File"
I am getting error:-
/usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- first.rb (LoadError)
from /usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from second.rb:2:in `<main>'
Please tell us. How to solve this problem.

require_relative "image_utils"
Based on your Ruby version, using require assumes that image_utils.rb is in the $LOAD_PATH (this requires additional setup) as Ruby 1.9 has removed the current directory from the load path. Use require_relative instead.
Ruby will first try to resolve the file by its absolute path. Then, if it isn't found then it will check on the $LOAD_PATH as mentioned above, if not then it will throw a LoadError
http://ruby-doc.org/core-2.0.0/Kernel.html#method-i-require

Related

LoadError - cannot open shared object file - file is present, but it says no such file

Ruby comes up with LoadErrors I do not understand. It complains about opening a shared object file, while it's present.
irb(main):001:0> require 'openssl'
LoadError: libssl.so.1.0.0: cannot open shared object file: No such file or directory - /usr/lib/ruby/2.3.0/x86_64-linux/openssl.so
from /usr/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /usr/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /usr/lib/ruby/2.3.0/openssl.rb:13:in `<top (required)>'
from /usr/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /usr/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
but ls /usr/lib/ruby/2.3.0/x86_64-linux/openssl.so
returns the file /usr/lib/ruby/2.3.0/x86_64-linux/openssl.so
Load Path:
irb(main):001:0> pp $LOAD_PATH
["/usr/lib/ruby/site_ruby/2.3.0",
"/usr/lib/ruby/site_ruby/2.3.0/x86_64-linux",
"/usr/lib/ruby/site_ruby",
"/usr/lib/ruby/vendor_ruby/2.3.0",
"/usr/lib/ruby/vendor_ruby/2.3.0/x86_64-linux",
"/usr/lib/ruby/vendor_ruby",
"/usr/lib/ruby/2.3.0",
"/usr/lib/ruby/2.3.0/x86_64-linux"]
Another example, with the rails gem installed, generating a new project fails with following LoadError.
/usr/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:120:in `require': libcrypto.so.1.0.0: cannot open shared object file: No such file or directory - /usr/lib/ruby/2.3.0/x86_64-linux/digest/md5.so (LoadError)
And again, doing ls /usr/lib/ruby/2.3.0/x86_64-linux/digest/md5.so shows the presence of the file.
my ruby version is ruby 2.3.1p112.
In case you wonder, uname -m returns x86_64.
I must miss something obvious, I guess. Any hints popping up are much appreciated!
Thanks to jordanm's comment, I was able to solve the issue!
The issue was related to openssl. ldd prints shared object dependencies and revealed the missing libraries.
ldd /usr/lib/ruby/2.3.0/x86_64-linux/openssl.so
...
libssl.so.1.0.0 => not found
libcrypto.so.1.0.0 => not found
...
After installing openssl-1.0 package, (while openssl v1.1.0 package was installed), the output of the same command looks better:
libssl.so.1.0.0 => /usr/lib/libssl.so.1.0.0 (0x00007faddac8f000)
libcrypto.so.1.0.0 => /usr/lib/libcrypto.so.1.0.0 (0x00007fadda814000)
and now, I'm able to require 'openssl' as well as generating a new rails project.
But after all, shouldn't ruby complain about missing packages, or should openssl-1.0 be at least a dependency of rails?

Clarification of what Ruby's 'require' does

A heads up that I'm in no way a Ruby expert...I just use it from time to time for basic scripting. I'm trying to use the RubyCocoa framework so that I can use additional commands in my Ruby script.
As an example, if I was to try something as explicit as this:
#!/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby
require '/System/Library/Frameworks/RubyCocoa.framework/Versions/A/Resources/ruby/osx/cocoa.rb'
puts "Hello, World"
I receive this error:
/usr/local/Cellar/ruby/1.9.3-p194/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- osx/objc/cocoa.rb (LoadError)
from /usr/local/Cellar/ruby/1.9.3-p194/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /System/Library/Frameworks/RubyCocoa.framework/Versions/A/Resources/ruby/osx/cocoa.rb:8:in `<top (required)>'
from /usr/local/Cellar/ruby/1.9.3-p194/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /usr/local/Cellar/ruby/1.9.3-p194/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from Test.rb:4:in `<main>'
I do have Homebrew installed on my Mac (running 10.10), but why does the require command go through to the Cellar folder? You can see I'm trying to use the 2.0 Ruby version from my Frameworks folder (the one in /usr/bin is still 1.9.3 (would also love for someone to explain how these versions differ and why)).
It goes through the Cellar folder because it is in your load path.
You can examine your load path by writing in your script
puts $:
Concerning your error message
You can use fully qualified path in require so
require '/System/Library/Frameworks/RubyCocoa.framework/Versions/A/Resources/ruby/osx/cocoa.rb'
is correct.
And indeed in your error message you can see this line
from /System/Library/Frameworks/RubyCocoa.framework/Versions/A/Resources/ruby/osx/cocoa.rb:8:in `<top (required)>'
So you correctly required your file
However from the first line of your error message.
usr/local/Cellar/ruby/1.9.3-p194/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- osx/objc/cocoa.rb (LoadError)
We can see ruby/osx/cocoa.rb tries to require another file. This one is different and found under osx/objc/cocoa.rb. So those files have similar names but are different.
And since it tries to require with require 'osx/objc/cocoa.rb' it fails because it does not know how to find it (not a fully qualified path here).
Maybe you can try to change the load path variable.
By adding the line:
$: << '/System/Library/Frameworks/RubyCocoa.framework/Versions/A/Resources/ruby/osx/objc/'
It might work but I’m not sure since I don’t know where that osx/obj folder is located on your machine.
you might want to take some time to clean your ruby installation and maybe instead a fresh ruby using rvm or rbenv (I prefer rbenv)

`require': cannot load such file -- hello (LoadError)

I am attempting to run a test against a hello method contained within the hello file:
ruby hello_spec.rb
which returns:
/usr/local/Cellar/ruby/2.1.2_2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- hello (LoadError)
from /usr/local/Cellar/ruby/2.1.2_2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from hello_spec.rb:116:in `<main>'
The files are contained within the same directory. I've installed RSpec and (I believe) the necessary gems. Other people seem to have similar problems but none of the solutions have worked for me.
I am running Ruby 2.1.2
I am new to Ruby and am struggling (obviously) to get the environment properly configured. Any help is much appreciated.
Note: I didn't write any of the test code. I've literally only made the hello.rb file.
Change require 'hello' to require_relative 'hello' in your hello_spec.rb. Current directory is not included in default ruby load path by default.
Or, alternatively, add the current directory to ruby load path:
$:.unshift File.dirname(__FILE__)
Hope it helps.

RUBY 2.0.0 How to run a file in irb, so that all its methods are parts of irb environment?

I know the command for this is: require file name
but I guess this command is for ruby 1.9.4 and I am using ruby 2.0.0
the exact message is.
$require start.rb
LoadError: cannot load such file -- start.rb
from /home/aka/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'
from /home/aka/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'
from (irb):2
from /home/aka/.rvm/rubies/ruby-2.0.0-p247/bin/irb:13:in `<main>'
Thanks in advance
Inside irb
>> require './start'
=> true
The file start.rb being in the same folder as you are, and the required file being in quotes, prefaced with the relative location ./ as shown.
This should work for you in Ruby from 1.8 on to 2.0 and beyond.
According to the docs, require will look for start.rb in the $LOAD_PATH since it does not resolve to an absolute path. In this case, the LoadError is telling you that start.rb is not in your $LOAD_PATH.
I am guessing that start.rb is in your current directory. You can use
>> require '/path/to/start.rb'
or
>> require './start.rb'
or
>> require './start'
This saves you some typing:
$ irb -r ./start.rb

Why does Ruby find all of my classes except for one named Config?

I have a Ruby program that runs fine on Linux. I'm trying it out on Windows 7 right now, and it should be fine since it only uses two libraries that installed without issues.
The error I'm getting is related to my own code. I have a file called config.rb, which has a class named Config. It has some values that you can change. Sounds pretty harmless.
However, I'm unable to require this class. Ruby gems custom require (i dont use gems at all) is not finding my file. What is going on here?
<internal:lib/rubygems/custom_require>:29:in `require': no such file to load -- config (LoadError)
from <internal:lib/rubygems/custom_require>:29:in `require'
from apitester.rb:9:in `<main>'
On line 9 of apitester.rb I have:
require 'config'
and config.rb is that simple class, in the same folder.
Try with the following in Ruby 1.8:
require File.join(File.dirname(__FILE__), 'config')
or if you are using in Ruby 1.9:
require_relative 'config'

Resources