Gem not working in rails project but in ruby file - ruby-on-rails

When I run test.rb (see below) as a separate ruby file from within my rails project it works fine but when I wrap it as a module to be called from a controller it gives me:
LoadError (no such file to load -- eventmachine):1 in 'ModuleTest'
The gem is installed (sudo gem install event machine and bundle install) and added to the gem file (gem 'eventmachine').
Could someone please advice on what it is that I'm missing?
Separate file (called through: $ ruby lib/test.rb):
require 'rubygems'
require 'eventmachine'
require 'em-http'
require 'fiber'
def doStuff
end
doStuff
Module:
require 'eventmachine'
require 'em-http'
require 'fiber'
module ModuleTest
def doStuff
end
end
Controller:
require 'moduletest'
class MyController < ApplicationController
doStuff
end

Add
gem 'eventmachine'
Into your Gemfile. Then execute bundle install.
If you want to use a new gem in your rails project, you need to add it into Gemfile. To learn more, visit http://gembundler.com/

Related

How to load a Ruby gem into a module

I am trying to write a web scraper using Watir that can be run on a schedule.
My module is called PriceScraperModule but it is not loading. I get this error:
NameError (uninitialized constant PriceScraperModule::Watir)
My module looks like:
module PriceScraperModule
def self.scrape
browser = Watir::Browser.new
end
end
My Gemfile includes:
gem 'watir'
gem 'webdrivers'
When I try requiring it, it doesn't work either:
module PriceScraperModule
require 'watir'
def self.scrape
browser = Watir::Browser.new
end
end
I get this error:
LoadError (cannot load such file -- watir)
What should I do?
I have written the following code and it's working properly.
require 'watir'
module PriceScraperModule
def self.scrape
b = Watir::Browser.new
b.goto 'www.google.com'
end
end
PriceScraperModule.scrape
Fixed it. In your terminal, run
$ spring stop
I was just checking as I did comment with perspective of Rails but you are not using rails so in case if you want to use it as a individual ruby project with bundler following will work.
Gemfile
# frozen_string_literal: true
source 'https://rubygems.org'
git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
gem 'watir'
gem 'webdrivers'
your module price_scraper_module.rb file
module PriceScraperModule
require 'watir'
def self.scrape
browser = Watir::Browser.new
browser.goto 'www.google.com'
end
end
and now any file where you want to use I am using sample.rb
#!/usr/bin/ruby
$LOAD_PATH << '.'
require 'price_scraper_module'
include PriceScraperModule
PriceScraperModule.scrape
Now just run following commands to get all Gems
bundle
and to run the sample
ruby sample
Although your one file is perfectly working, in case you want to make it part of an individual project with Gemfile.

Rails gem developement coping resources to the application

In my gem (myEngine.rb) I have the following code :
require "myEngine/version"
require "myEngine/models/contact"
require 'rails/generators'
module myEngine
def self.generate_migration
file_name= "migrationSrc.rb"
src_path= "myEngine/migration_files/"+file_name
destination_path= "db/migrate/"+file_name
//
end
end
When I call the method generate_migration I want my gem to copy the migration file to the db/migrate of my application.
Thanks you for your help.

How do I create a command line rails generator gem?

I know how to create a rails generator gem which is called like:
rails g my_generator command
And I know how to create a thor gem which can be called like:
my_generator command
But I don't know how to create a rails generator that can be called using an executable. I have tried by creating a lib/my_generator/cli.rb file like:
require 'thor'
module Mang
class Cli < Thor
include ::Rails::Generators::Base
desc "install_gem", "install a gem"
def install_gem
gem 'thor', "0.18.1"
end
end
end
But I get the following error despite having added Rails as a dependency in my gemspec.
uninitialized constant Rails (NameError)
The fix was just a matter of including the rails/generators/actions module.
require 'thor'
require 'rails/generators'
require 'rails/generators/actions'
module Mang
class Cli < Thor
include Thor::Actions
include Rails::Generators::Actions
desc "install_gem", "install a gem"
def install_gem
gem 'thor', "0.18.1"
end
end
end

Cannot find file or constant when using a gem locally in a Rails app

I have a gem that I am making and it is located at the same level as a rails app.
I am trying to use the gem in the rails app, but am not able to make it work - the constant I am trying to use, which is defined in the gem, is not accessible to the rails app for some reason.
What am I doing wrong here? What steps can I take to begin debugging the cause of the problem?
In the rails console, $:.grep /mygem/ shows me ["/Users/zabba/mygem/lib"]
Directory structure, with the contents of certain files:
~/mygem/
lib/
mygem/
some_class.rb
module Mygem
class SomeClass
end
end
mygem.rb
require 'mygem/some_class'
~/railsapp/
Gemfile:
gem 'mygem', path: '../mygem', require: 'mygem'
app/
models/
a_model.rb:
# require 'mygem' cannot find file
class AModel
def hello_world
SomeClass.new # Cannot find constant
Mygem::SomeClass.new # Cannot find constant
end
end
Can you create a folder vendor/gems and copy your gem in there. Then in you Gemfile
Gemfile
gem 'mygem', :path => "vendor/gems/mygem-0.0.1"
Then bundle install

require other modules in the controller

gem install rubyoverflow
irb
> require 'rubyoverflow'
=> true
But:
require 'rubyoverflow'
include Rubyoverflow
class QuestionsController < ApplicationController
def question_by_tag
ruby_q = Questions.retrieve_by_tag('ruby')
Get error:
LoadError in
QuestionsController#question_by_tag no
such file to load -- rubyoverflow
Rails.root:
D:/artefacts/dev/projects/stack
app/controllers/questions_controller.rb:1:in
`'
This error occurred while loading the
following files: rubyoverflow
Is there any special rules to import moduled in the controller?
why do you use both require and include? include Rubyoverflow will be enough
UPD
For gem you should add it into your Gemfile (Rails 3.x) or config/environment.rb (Rails 2.x)
# Gemfile
gem "rubyoverflow"
# environment.rb
config.gem "rubyoverflow"
Then run bundle for Rails 3.x and rake gems:install for Rails 2.x

Resources