LoadError: cannot load such file -- directory/file_name - ruby

I have a file structure like
root
|--lib
|--root.rb
|--extensions
|--strings.rb
I want to be able to use methods in string.rb in root.rb file.
So I added require 'extensions/strings' at the top of root.rb file.
But I get LoadError: cannot load such file -- extensions/strings.rb error.
How do I avoid this?

You can try the below:
require File.dirname(__FILE__) + "/extensions/strings"

Use require_relative if you are in Ruby 1.9 or later. In root.rb, write:
require_relative 'extensions/strings.rb'

I found the answer I am looking for here.
I used jandot's solution.
Dir[File.dirname(__FILE__) + '/extensions/*.rb'].each {|file| require file }
Edit after some testing,
For some reason, this doesn't throw any error message, but it doesn't seem to be loading the actual Ruby file.
I tried adding this to extensions/strings.rb
class Dog
def self.bark
puts "bark"
end
end
And ran it on irb.
1.9.3-p0 :001 > require 'rhapsody'
=> true
1.9.3-p0 :002 > Dog
NameError: uninitialized constant Dog
from (irb):2
from /Users/jasonkim/.rvm/rubies/ruby-1.9.3-p0/bin/irb:16:in `<main>'
So it's not finding the Dog class in extensions/strings.rb for some reason.
Edit after reading a guid in rubygems.org
When I start irb, I had to go irb -Ilib -rextensions
The guide explains the situation this way
We need to use a strange command line flag here: -Ilib. Usually RubyGems includes the lib directory for you, so end users don’t need to worry about configuring their load paths. However, if you’re running the code outside of RubyGems, you have to configure things yourself.

require_relative '../lib/extensions/strings'
Works for me.

Related

Automatically load Dotenv on my ruby console

I'd like to automatically run Dotenv.load('.env.development') whenever I launch up a ruby console, it could either be from bundle console or alternatively irb. I'm using Sinatra, not Rails, and I'm not sure how to run some commands on console start.. I'd prefer to do this without a bash script, instead using the internal capabilities of the tools.. If there's a place to put ruby code on the start of a ruby console that would solve my issue and also allow for future console customization.
You can create a .irbrc file in our project's directory which is loaded automatically when an IRB session is started. And add something like this to that file:
begin
require 'dotenv'
Dotenv.load('.env.development')
rescue => e
puts "loading Dotenv failed. because: #{e.message}"
end
Read more about the .irbrc file in the Ruby-Docs.
You try folliwing the documentation of gem(sorry my ignorance I don't know anything about sinatra)?:
Documentation dotenv
Install the gem:
$ gem install dotenv
By default, load will look for a file called .env in the current working directory. Pass in multiple files and they will be loaded in order. The first value set for a variable will win.
require 'dotenv'
Dotenv.load('file1.env', 'file2.env')
In your case i think should be:
require 'dotenv'
Dotenv.load('.env.development')
In ruby vanilla I dont know if possible, I think yes.
One option is to creating a ./bin/console script ala Bundler's gem.
I created this bin/console file as a temporary solution, but I'm curious whether I can get #spickermann's answer (which I incorporated here) with irbrc to work with a same-directory .irbrc
#!/usr/bin/env ruby
begin
require 'dotenv'
Dotenv.load('.env.development')
rescue => e
puts "loading Dotenv failed. because: #{e.message}"
end
require "irb"
IRB.start(__FILE__)

Do the .rb files in your db >>migrate have to be the same name as the rb files in your model folder?

I am fresh into Sinatra and Activerecord and I noticed I get a lot of errors such as
LoadError: cannot load such file -- ./model/character_houses
or
rake aborted!
NameError: uninitialized constant House
The first one is when I try to load into irb with require './app' for my main rb file.
The second is when I try to load a seed file.
Could someone just explain how file structures should be linked when using Sinatra and ActiveRecord. I have no problem setting the files up it's only when I try to check within irb or actually fill the tables.
A lot of the forums I see online pertain mostly to ruby on rails but we as a class are starting that after this so I am not sure if it is similar or relevant to my situation.
With sinatra the 'require' order is important
Dir.glob('./app/{exceptions,helpers}/*.rb').each do |file|
require file
end
require './app/controllers/api_controller'
require './app/uploaders/application_uploader'
Dir.glob('./app/{uploaders,jobs,controllers,models,etls,docs}/*.rb').each do |file|
require file
end
try 'require_relative app' with irb

Unable to load modules in ruby

I googled it, but no luck
I have a file called mdl.rb and main.rb, they both in the same folder
mdl.rb has module Test_module and method in it called say_hello, I want to use that method in my main.rb.
So my main.rb looks like this:
require 'mdl'
say_hello
but I get error:
in `require': cannot load such file -- mdl (LoadError)
You would use
require_relative 'mdl.rb'
See Ruby docs for require_relative.
(require_relative 'mdl' and require './mdl' also work, as pointed out in the comments by #MichaelBerkowski and #FélixSaparelli respectively.)

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.

Ruby mixin gives unidentified constant error

In irb, I do this
class Text
include FileUtils
end
I get: NameError: uninitialized constant Test::FileUtils
If I just do: include FileUtils (i.e. now class) everthing works.
What gives?
You need to make sure Ruby knows about the FileUtils module. That module isn't loaded by default:
>> FileUtils
NameError: uninitialized constant FileUtils
from (irb):1
>> require 'fileutils'
=> true
>> FileUtils
=> FileUtils
Don't worry too much about the error NameError: uninitialized constant Text::FileUtils - when you try to include a constant that Ruby doesn't know about, it looks in a few places. In your case, first it will look for Text::FileUtils and then it will look for ::FileUtils in the root namespace. If it can't find it anywhere (which in your case it couldn't) then the error message will tell you the first place it looked.
Did you try?
class Text
include ::FileUtils
end
This assumes that FileUtils is not within a module.
This is an old thread, but still if any bumps on this thread to find an answer. One just needs to add below line on top of his code (or anywhere outside the class/method/module)
require 'fileutils'
Including in the class does not works, may be it used to work in older versions.

Resources