Why do do I get a "LoadError"? - ruby-on-rails

I have two Ruby scripts, and I am calling those files and I am getting an error.
I have r1.rb and r2.rb. When I call r1.rb and r2.rb from r3.rb I get this error:
C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- r1 (LoadError)
from C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from r3.rb:1:in `<main>'
r1.rb is:
def random
rand(1000000)
end
r2.rb is:
def random
(rand(26) + 65).chr
end
r3.rb is:
require 'r1'
require 'r2'
puts random

thumb rules when you use require, always use path in require statement [mostly when you use windows platform]
if you use load you need to place filename.rb but no need to pass file path. [First Check the requirement and use load because it is load every time in memory when call]
here I am modifying your code
require 'C:/PLACE YOUR FILE PATH HERE/r1'
require 'C:/PLACE YOUR FILE PATH HERE/r2'
puts random
Please let me know if this works for you
let me add one more things
Same way when you use irb, you have to do same way
>irb
irb(main):001:0> require 'c:/rubycode/test.rb'
=> true
#jdoe is suggest require_relative that's good approach
but I am getting below error
irb(main):001:0> require_relative 'test'
LoadError: cannot infer basepath
from (irb):1:in `require_relative'
from (irb):1
from C:/Ruby193/bin/irb:12:in `<main>'
irb(main):002:0> require_relative 'rubycode/test'
LoadError: cannot infer basepath
from (irb):2:in `require_relative'
from (irb):2
from C:/Ruby193/bin/irb:12:in `<main>'
irb(main):003:0> require_relative 'c:/rubycode/test'
LoadError: cannot infer basepath
from (irb):3:in `require_relative'
from (irb):3
from C:/Ruby193/bin/irb:12:in `<main>'
irb(main):004:0> require 'c:/rubycode/test'
=> true
irb(main):005:0>

Related

how to require a gem in rails test?

How do you test gem functionality? I'm trying to figure out why something isn't working the way the docs suggest, and set up my own version of these. Yet my test fail:
19: ...
18: ...
#...etc....
5: from /Users/me/code/project/test/shrine_test.rb:5:in `<top (required)>'
4: from /Users/me/code/project/test/shrine_test.rb:5:in `require'
3: from /Users/me/.rvm/gems/ruby-2.5.0/gems/lockbox-0.4.8/lib/lockbox.rb:20:in `<top (required)>'
2: from /Users/me/.rvm/gems/ruby-2.5.0/gems/lockbox-0.4.8/lib/lockbox.rb:20:in `require'
1: from /Users/me/.rvm/gems/ruby-2.5.0/gems/lockbox-0.4.8/lib/lockbox/railtie.rb:1:in `<top (required)>'
/Users/me/.rvm/gems/ruby-2.5.0/gems/lockbox-0.4.8/lib/lockbox/railtie.rb:2:in `<module:Lockbox>': uninitialized constant Rails::Railtie (NameError)
Did you mean? Rails
This is my test file, shrine_test.rb:
require "bundler/setup"
Bundler.setup
require "minitest/autorun"
require "minitest/pride"
require "lockbox" #this is the line 5 from above
$logger = ActiveSupport::Logger.new(ENV["VERBOSE"] ? STDOUT : nil)
require_relative "support/shrine"
require_relative "support/active_record"
Lockbox.master_key = SecureRandom.random_bytes(32)
class ShrineTest < Minitest::Test
#tests here
end
Assuming the gem in your Gemfile is in the default group (not in any group :environment do block), replace the Bundler.setup with
Bundler.require(:default)

When I run the test “ rspec spec/cart_spec.rb ”In the console, I'll get a warning

When I run the test “ rspec spec/cart_spec.rb ”In the console, I'll get a warning
require 'rspec'
require_relative '../app/item'
require_relative '../app/virtual_item'
require_relative '../app/antique_item'
require_relative '../app/item_container'
require_relative '../app/cart'
describe Cart do
it 'add items into the cart' do
cart = Cart.new('')
item1 = Item.new('kettle', price: 200)
item2 = Item.new('car', price: 200)
cart.add_items(item1, item2)
cart.items.should include(item1, item2)
end
end
In the console, I'll get a warning
E:\work\storeapp\spec>rspec spec/cart_spec.rb
c:/tools/rubies/ruby-2.1.5-p273/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.2/lib/rspec/core/configuration.rb:1226:in `load': cannot load such file -- E:/work/storeapp/spec/spec/cart_s
pec.rb (LoadError)
from c:/tools/rubies/ruby-2.1.5-p273/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.2/lib/rspec/core/configuration.rb:1226:in `block in load_spec_files'
from c:/tools/rubies/ruby-2.1.5-p273/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.2/lib/rspec/core/configuration.rb:1224:in `each'
from c:/tools/rubies/ruby-2.1.5-p273/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.2/lib/rspec/core/configuration.rb:1224:in `load_spec_files'
from c:/tools/rubies/ruby-2.1.5-p273/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.2/lib/rspec/core/runner.rb:97:in `setup'
from c:/tools/rubies/ruby-2.1.5-p273/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.2/lib/rspec/core/runner.rb:85:in `run'
from c:/tools/rubies/ruby-2.1.5-p273/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.2/lib/rspec/core/runner.rb:70:in `run'
from c:/tools/rubies/ruby-2.1.5-p273/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.2/lib/rspec/core/runner.rb:38:in `invoke'
from c:/tools/rubies/ruby-2.1.5-p273/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.2/exe/rspec:4:in `<top (required)>'
from c:/tools/rubies/ruby-2.1.5-p273/bin/rspec:23:in `load'
from c:/tools/rubies/ruby-2.1.5-p273/bin/rspec:23:in `<main>'
![enter image description here][1]
where is my mistake?
The error says that it can't find the spec file and indeed you're pointing to
spec/cart_spec.rb
... while in the spec directory. To find it from there you need to remove spec/:
rspec cart_spec.rb
My usual practice is to run specs from the app root. From there, your original command should work. Or you can just use an absolute path instead of a relative path.

Rails i18n spec doesn't work

In my rails app, I use the gem i18n_spec to add rspec tests that check if my i18n config files are correct.
The spec file
require 'spec_helper.rb'
require 'i18n-spec/tasks'
Dir.glob('config/locales/*.yml') do |locale_file|
describe "#{locale_file}" do
it { subject.should be_parseable }
results in errors
../gems/i18n-spec-0.6.0/lib/i18n-spec/tasks.rb:5:in `<top (required)>': undefined method `namespace' for main:Object (NoMethodError)
from ../gems/backports-3.3.3/lib/backports/tools.rb:328:in `require'
from ../gems/backports-3.3.3/lib/backports/tools.rb:328:in `require_with_backports'
from ../gems/activesupport-3.2.21/lib/active_support/dependencies.rb:251:in `block in require'
from ../gems/activesupport-3.2.21/lib/active_support/dependencies.rb:236:in `load_dependency'
from ../gems/activesupport-3.2.21/lib/active_support/dependencies.rb:251:in `require'
from ../spec/selftest/i18n_spec.rb:3:in `<top (required)>'
Why do you want to test if they are parseable? If you are asserting any I18n key in a unit / feature spec Rails will load your I18n files and throw a syntax error if the yml files aren't parseable!
You will get assertions like so:
within(".mod-header .login") { click_link I18n.t('public.menu.login') }
find("#main-container h1").should have_content I18n.t('pages.login.title')
This way Rails will load and parse your yml files. And if there are any errors you will find them in your test output!
And errors like this:
<Psych::SyntaxError: (your_project_path/config/locales/nl.yml): mapping values are not allowed in this context at line <num> column <num>>
or
<Psych::SyntaxError: (your_project_path/config/locales/nl.yml): did not find expected key while parsing a block mapping at <num> column <num>>

Access my model using a daemon on rails 3

I have the following script:
#!/usr/bin/ruby
require 'rubygems' unless defined?(Gem)
require 'mongoid'
include Mongoid::Document
#classes = Availability.where(:availability_date.gt => Time.now.utc + 1.hours).to_a
puts #classes.count
But I always get:
classes_notification.rb:6:in `': uninitialized constant Availability (NameError)
Some help please, I need to make a daemon to send emails with the information on my availability model.
Thanks in advance.
UPDATE CODE
my script is under app/script/user/remeber_classes.rb
how can I access the require File.dirname(FILE) + "/../../config/environment"
because I'm getting
/Users/jeanosorio/.rvm/rubies/ruby-1.9.3-p484/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:126:in
require': cannot load such file -- ./../../config/environments
(LoadError) from
/Users/jeanosorio/.rvm/rubies/ruby-1.9.3-p484/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:126:in
require' from remember_class.rb:5:in `'
you must include rails environment first
add require "path_to_the_application/config/environment" to your code
or
you can always access the database directly using mongo gem
see this
https://github.com/mongodb/mongo-ruby-driver/wiki/Tutorial

uninitialized constant LoginController in Ruby.

I am newbie in Ruby. I installed by following this tutorial: http://udooz.net/blog/2011/02/facebook-app-using-rails-koala/
Now, when I do this: rails generate controller
rails generate controller home index
I get this error:
/home/hiccup-pro/Documents/qstack/config/environment.rb:8:in `<top (required)>': undefined local variable or method `config' for main:Object (NameError)
from /home/hiccup-pro/.rvm/gems/ruby-1.9.2-p320/gems/railties-3.2.3/lib/rails/application.rb:103:in `require'
from /home/hiccup-pro/.rvm/gems/ruby-1.9.2-p320/gems/railties-3.2.3/lib/rails/application.rb:103:in `require_environment!'
from /home/hiccup-pro/.rvm/gems/ruby-1.9.2-p320/gems/railties-3.2.3/lib/rails/commands.rb:25:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
I fixed it. Atleast that bug, but now I've a different bug and that is: uninitialized constant LoginController. I googled a bit and found out that I should not mention config.* in the environment.rb.
Anyway,
this is my environment.rb
# Load the rails application
require File.expand_path('../application', __FILE__)
# Initialize the rails application
Qack::Application.initialize!
config.action_controller.allow_forgery_protection = false
So, why do I get this uninitialized constant LoginController error?
Without seeing your environment.rb is hard to say, but my best shoot is that you put the code
config.action_controller.allow_forgery_protection = false
config.gem "koala"
Outside of the block
Rails::Initializer.run do |config|
....
end
That is inside of environment.rb. If there is no such a block add it like this:
Rails::Initializer.run do |config|
config.action_controller.allow_forgery_protection = false
config.gem "koala"
end

Resources