Is referencing using: require '../db.rb' correct? - ruby

I need to reference my db.rb file that is lower in the directory tree.
/folder1/test.rb
/db.rb
/database.yml
running:
ruby db.rb
doesn't return an error.
In test.rb I did:
require '../db.rb'
and I get an error:
../db.rb:7:in `initialize': No such file or directory - database.yml (Errno::ENOENT)
from ../db.rb:7:in `open'
from ../db.rb:7
from test.rb:1:in `require'
from test.rb:1
Not sure what the issue is here?

The relative require itself is okay. However, require doesn't change your current working directory. So it's looking for database.yml in /folder1.

In previous Rubies, require included the calling script's directory. There were security issues with that so the '.' directory was removed in 1.9. To provide a way of getting to things in the local directory require_relative was added.
http://svn.ruby-lang.org/repos/ruby/tags/v1_9_2_0/NEWS talks about its inclusion into Core.

Related

Rspec a file which requires others. `require': cannot load such file

Is it possible to run an rspec on a file which requires other files?
my .rb file has the following lines:
require "colorize"
require "./board_initializer"
require "./pieces"
and when running a rake I get the following error:
.rvm/rubies/ruby-2.2.5/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- ./board_initializer (LoadError)
Thanks!
The error means board_initializer.rb isn't in the current working directory for __FILE__. Some ways to resolve this include:
Providing a valid filename argument to require.
Using require_relative with a valid relative path.
Modifying the current LOAD_PATH.
There are certainly other ways to resolve this, but they all amount to ensuring that board_initializer.rb can be found by the interpreter when you load or require the file.

Compile ruby script with dependencies on other classes

have a problem with compressing my script.
I have a main.rb and some classes in subfolders like Subfolder/Class.rb
In my main.rb, I have the Classes declared like that:
require './Subfolder/Class.rb'
When I just run my main script, it works. Also my exe works, when it is in the same place as the main.rb.
But when I put the exe somewhere else I get this error:
C:/Users/MLEING~1/AppData/Local/Temp/ocr53C2.tmp/lib/ruby/site_ruby/1.9.1/rubyge
ms/custom_require.rb:36:in `require': cannot load such file -- ./Parsing/Calibra
tionState (LoadError) from C:/Users/MLEING~1/AppData/Local/Temp/ocr53C2.tmp/lib/ruby/site_ruby
/1.9.1/rubygems/custom_require.rb:36:in `require'
from C:/Users/MLEING~1/AppData/Local/Temp/ocr53C2.tmp/src/main.rb:9:in `
<main>'
Can I somehow put the dependencies into my exe?
I also tried to include them like that:
ocra main.rb Subfolder/*.rb
But it doesn't help.
Have you tried making a ruby gem out of your project? http://guides.rubygems.org/make-your-own-gem/
Gems define their own dependencies.
Your require is using a relative path from the current directory (which you can see because it starts with "./"
Instead, try:
require 'Subfolder/Class.rb'
And make sure $LOAD_PATH includes the location where all of your ruby code is unpacked (which you can look at by examining $0 (or figure out the full path from $0 and require the .rb with a full path)

ruby require not working

I'm new to ruby, but I'm working on my first ruby program. It currently has two files, one is a library of functions (xgync.rb stored in lib) the other is the executable xgync stored in 'bin'. (Project visible here https://bitbucket.org/jeffreycwitt/xgync/src) I've also created a symlink to my /usr/local/bin/xgync so that I can write the command xgync {arguments} from anywhere in the terminal.
The problem seems to be that bin/xgync depends on the library lib/xgync.rb. I've written this dependency in bin/xgync as follows:
$:.unshift(File.dirname(__FILE__) + '/../lib')
require "xgync"
However, i keep getting the following error:
/Users/JCWitt/.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 -- xgync (LoadError)
from /Users/JCWitt/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /usr/local/bin/xgync:4:in `<main>'
can you see anything wrong with what I've written? Could the symlink be somehow messing things up?
Thanks for your help :)
When using ruby 1.9.x you don't usually alter the path with the $:.unshift when requiring other files in your project.
Instead the best practice is to use require_relative instead.
require_relative '../lib/xgync.rb'
require_relative requires files relative to the file you are currently editing.
But the error you experience appears, because you require a file, which does not exist:
bin/xgync
lib/xgync.rb
These are the files in your project according to your question, and the code-excerpt is from bin/xgync you extended the path to look for files in lib/ but you try to require 'xgync' which is a file, that is not present in lib/, so if you wanted to use this method (instead of require_relative you would have to use require 'xgync.rb'.

stuck in rvm hell trying to get a simple rspec running

Ruby Settings From terminal
% ruby -v
ruby 1.9.2p180 (2011-02-18 revision 30909) [i686-linux]
=> ~/ruby/grounded/test
% where ruby
/home/mike/.rvm/rubies/ruby-1.9.2-p180/bin/ruby
/home/mike/.rvm/bin/ruby
/usr/local/bin/ruby
/usr/bin/ruby
=> ~/ruby/grounded/Person/test
% which ruby
/home/mike/.rvm/rubies/ruby-1.9.2-p180/bin/ruby
% rvm current
ruby-1.9.2-p180
Directory Structure
% tree
.
├── bowling.rb
└── bowling_spec.rb
File Contents
bowling.rb
class Bowling
end
bowling_spec.rb
require 'rubygems'
require 'rspec'
require 'bowling'
describe Bowling, "#score" do
it "returns 0 for all gutter game" do
bowling = Bowling.new
20.times { bowling.hit(0) }
bowling.score.should eq(0)
end
end
% ruby bowling_spec.rb
/home/mike/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': no such file to load -- bowling (LoadError)
from /home/mike/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from bowling_spec.rb:3:in `<main>'
Questions
Why am I getting a LoadError when bowling.rb and bowling_spec.rb
are in the same folder?
In the error what the heck is .../site_ruby/1.9.1/... when I am running ruby 1.9.2 then why would 1.9.1 even show up?
how do I get over this hump and start having fun with ruby.
When requiring files, the file must be in a list of directories called the $LOAD_PATH. The current directory used to be in this list, but as of 1.9.2, it has been removed for security reasons.
You have four options (listed in order of how good I think they are)
1 Change your directory structure to look like this
.
|-- lib
| `-- bowling.rb
`-- spec
`-- bowling_spec.rb
And then run as rspec spec instead of ruby bowling_spec.rb
This works because RSpec will see that lib is in your current directory, and then add it to the $LOAD_PATH for you.
If you do this, you also don't have to require 'rspec'.
2 Run with ruby -I . bowling_spec.rb
which will add the current directory to the $LOAD_PATH
3 Use require_relative 'bowling' instead of require 'bowling'.
This will look for the bowling.rb file relative to the current file being run (bowling_spec.rb)
4 Use require('../bowling', __FILE__) instead of require 'bowling'
This is basically the same as the above.
Other questions:
Q: Why am I getting a LoadError when bowling.rb and bowling_spec.rb are in the same folder?
A: Because the current directory (the directory you are running the script from, not the directory the files are located in) is not in the $LOAD_PATH.
Q: In the error what the heck is .../site_ruby/1.9.1/... when I am running ruby 1.9.2 then why would 1.9.1 even show up?
A: Hmm. Not sure I remember exactly, but IIRC, it was something like they're so similar that the interface hadn't changed, so they could be compatible with 1.9.1 from a system perspective.
Q: how do I get over this hump and start having fun with ruby.
A: I suppose that depends. If the issue is that you want to be able to run files that are in your CWD, then you can add the environment variable RUBYLIB to . to your .bash_profile (or whatever the equivalent is on your system) which will tell Ruby to look in the current directory for files. This is prone to bugs, though (and it could lead to unintentional execution of Ruby files, which is a security risk). If you just mean "how do I start learning" or whats a fun project? Then check out one of my projects, Ruby Kickstart which, in six sessions, will take you through a pretty big portion of Ruby, and have you write and deploy a simple web app by the end of it.
When you require a file and don't specify an absolutely path to the file, Ruby looks on its load path (accessed within ruby as $LOAD_PATH or $:) and checks each directory there for the file you want. You cannot load bowling.rb because it's not in a directory on your load path.
The solution is one of two things:
Put the current directory on the load path:
$:.unshift File.expand_path('.')
This puts the full path to the current working directory on the load path.
Use require with the absolute path to the file:
require File.expand_path('../bowling', __FILE__)
A little additional info: File.expand_path returns the absolute path to the first parameter from the current working directory, unless a second parameter is given; then it uses that as the starting point. So the whole line could be read:
require /home/mike/src/something/bowling_spec.rb/../bowling

Ruby Module Help -- Looking in wrong directory?

Hey everyone! I am having trouble with understanding modules -- I have two files, one named "modfile.rb" with the module, and one named "main.rb" that runs the code:
# modfile.rb
module Module1
def method1
puts "SUCCESS!"
end
end
# main.rb
require 'modfile'
Module1.method1
Unfortunately, instead of SUCCESS! appearing on my screen, I get this:
<internal:lib/rubygems/custom_require>:29:in 'require': no such file to load -- modfile (LoadError)
from <internal:lib/rubygems/custom_require>:29:in 'require'
from main.rb:1:in '<main>'
I think (though I may be wrong) that Ruby is looking to the lib/.... file inside the Ruby directory on my computer, while modfile.rb is saved in the same directory as main.rb. How do I fix this problem (other than by moving the module's file?)
PS. one guide suggested I add the line "require 'rubygems'" but I already did and got the same error.
Check into the differences between require and require_relative in Ruby: require vs require_relative - best practice to workaround running in both Ruby <1.9.2 and >=1.9.2
In Ruby 1.9 the . directory was removed from the search path. To fix the problem this generated they added require_relative.
If modfile.rb and main.rb are in the same directory, make sure that you aare calling main.rb from the directory it's in, ie:
ruby main.rb
As I believe that is the directory that the Ruby interpreter will be looking in for any require files.
Edit: as #the-tin-man points out, the behaviour has changed for Ruby 1.9.
To be completely on the safe side, you can do:
require File.join(File.dirname(__FILE__), "modfile")
One other thing:
def method1
... should be:
def self.method1
... since you are calling the method as a class level method.

Resources