Rails 3 runner not detecting JSON gem? - ruby

I'm trying to create Ruby script to run with my Rails 3 environment.
However, whenever I run this rubyscript with Rails Runner, I get this error:
require 'rubygems'
require 'json'
#payload = {
"message" => "helloworld",
"station" => {"id"=>12345}
}.to_json
puts #payload
ERROR:
/usr/lib/ruby/gems/1.8/gems/activesupport-3.0.0.beta4/lib/active_support/dependencies.rb:212:in `require': no such file to load -- json (LoadError)
from /usr/lib/ruby/gems/1.8/gems/activesupport-3.0.0.beta4/lib/active_support/dependencies.rb:212:in `require'
from /usr/lib/ruby/gems/1.8/gems/activesupport-3.0.0.beta4/lib/active_support/dependencies.rb:198:in `load_dependency'
from /usr/lib/ruby/gems/1.8/gems/activesupport-3.0.0.beta4/lib/active_support/dependencies.rb:554:in `new_constants_in'
from /usr/lib/ruby/gems/1.8/gems/activesupport-3.0.0.beta4/lib/active_support/dependencies.rb:198:in `load_dependency'
from /usr/lib/ruby/gems/1.8/gems/activesupport-3.0.0.beta4/lib/active_support/dependencies.rb:212:in `require'
from createflags.rb:2
from /usr/lib/ruby/gems/1.8/gems/railties-3.0.0.beta4/lib/rails/commands.rb:39:in `eval'
from /usr/lib/ruby/gems/1.8/gems/railties-3.0.0.beta4/lib/rails/commands/runner.rb:47
from /usr/lib/ruby/gems/1.8/gems/railties-3.0.0.beta4/lib/rails/commands.rb:39:in `require'
from /usr/lib/ruby/gems/1.8/gems/railties-3.0.0.beta4/lib/rails/commands.rb:39
from script/rails:6:in `require'
from script/rails:6
Can someone please tell me what I can do to fix this?

I'm not totally familiar with the internals of Bundler, but it may be that it doesn't allow requireing other gems not in the Gemfile. Maybe adding gem 'json' would take care of this.
Of course, ActiveSupport has its own JSON module, so there should be no need to pull in another gem.

remove require "json" and then try. I believe ActiveSupport already has to_json in built.

Related

Why gem is not loaded: require gems gives error no such file

I installed gem 'ssh-net'. From the console I tried to require the gem like this:
irb(main):009:0* require 'ssh-net'
LoadError: cannot load such file -- ssh-net
from /var/lib/gems/2.3.0/gems/activesupport-4.2.10/lib/active_support/dependencies.rb:274:in `require'
from /var/lib/gems/2.3.0/gems/activesupport-4.2.10/lib/active_support/dependencies.rb:274:in `block in require'
from /var/lib/gems/2.3.0/gems/activesupport-4.2.10/lib/active_support/dependencies.rb:240:in `load_dependency'
from /var/lib/gems/2.3.0/gems/activesupport-4.2.10/lib/active_support/dependencies.rb:274:in `require'
from (irb):9
from /home/csrhub/git/csrhub-api/bin/console:150:in `<top (required)>'
from /var/lib/gems/2.3.0/gems/bundler-2.1.1/lib/bundler/cli/exec.rb:63:in `load'
from /var/lib/gems/2.3.0/gems/bundler-2.1.1/lib/bundler/cli/exec.rb:63:in `kernel_load'
from /var/lib/gems/2.3.0/gems/bundler-2.1.1/lib/bundler/cli/exec.rb:28:in `run'
from /var/lib/gems/2.3.0/gems/bundler-2.1.1/lib/bundler/cli.rb:476:in `exec'
from /var/lib/gems/2.3.0/gems/bundler-2.1.1/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
from /var/lib/gems/2.3.0/gems/bundler-2.1.1/lib/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
from /var/lib/gems/2.3.0/gems/bundler-2.1.1/lib/bundler/vendor/thor/lib/thor.rb:399:in `dispatch'
from /var/lib/gems/2.3.0/gems/bundler-2.1.1/lib/bundler/cli.rb:30:in `dispatch'
from /var/lib/gems/2.3.0/gems/bundler-2.1.1/lib/bundler/vendor/thor/lib/thor/base.rb:476:in `start'
from /var/lib/gems/2.3.0/gems/bundler-2.1.1/lib/bundler/cli.rb:24:in `start'
from /var/lib/gems/2.3.0/gems/bundler-2.1.1/exe/bundle:46:in `block in <top (required)>'
from /var/lib/gems/2.3.0/gems/bundler-2.1.1/lib/bundler/friendly_errors.rb:123:in `with_friendly_errors'
from /var/lib/gems/2.3.0/gems/bundler-2.1.1/exe/bundle:34:in `<top (required)>'
from /usr/local/bin/bundle:23:in `load'
from /usr/local/bin/bundle:23:in `<main>'
Then I tried with a gem that works:
irb(main):023:0* require 'memcache'
=> false
I printed the gems locations:
csrhub#csrhub:~/git/csrhub-api$ bundle info 'net-ssh'
* net-ssh (5.2.0)
Summary: Net::SSH: a pure-Ruby implementation of the SSH2 client protocol.
Homepage: https://github.com/net-ssh/net-ssh
Path: /var/lib/gems/2.3.0/gems/net-ssh-5.2.0
csrhub#csrhub:~/git/csrhub-api$ bundle info 'memcache'
* memcache-client (1.8.5)
Summary: A Ruby library for accessing memcached.
Homepage: http://github.com/mperham/memcache-client
Path: /var/lib/gems/2.3.0/gems/memcache-client-1.8.5
csrhub#csrhub:~/git/csrhub-api$
I see that both gems are placed in the same directory. Why memcache is invokable and the ssh-net is not? What am I missing?
May be it has something to do with this ?
I installed gem 'ssh-net'.
No, you didn't. You installed the gem net-ssh, as you can clearly see in the output of bundle info that you posted:
csrhub#csrhub:~/git/csrhub-api$ bundle info 'net-ssh'
* net-ssh (5.2.0)
Summary: Net::SSH: a pure-Ruby implementation of the SSH2 client
Here you can see that the name of the gem is net-ssh and not ssh-net and that the name of the primary class is Net::SSH.
From the console I tried to require the gem like this:
irb(main):009:0* require 'ssh-net'
LoadError: cannot load such file -- ssh-net
The primary class in this gem is called Net::SSH. (It integrates in the Net namespace of the Ruby standard library, which has libraries like Net::FTP, Net::HTTP, Net::IMAP, Net::POP, Net::SMTP, and Net::Telnet.)
According to standard Ruby naming conventions, a class named Net::SSH should be located in a file named lib/ssh.rb. This is the file you need to require.
This is also shown in the very first line of the very first code sample of the very first page of the documentation:
require 'net/ssh'
I see that both gems are placed in the same directory. Why memcache is invokable and the ssh-net is not? What am I missing?
In 99.9% of all cases where a computer tells you it cannot find something, it is because that thing you told it to find is indeed not there. Computers are very, very good at finding things.
The same thing is the case here: you told Ruby
require 'ssh-net'
which means "go through each directory in the $LOAD_PATH and look for a file named ssh-net.rb". Such a file simply does not exist, ergo, you get a LoadError exception.

Cannot require RedCloth with irb

I am using Ruby22-x64 just to let you know, but I successfully installed RedCloth-4.2.9 using gem install RedCloth within the command prompt. When I try to require 'rubygems' I get =>false which is normal because it's already loaded, but when I try require 'RedCloth this is the following error code I get:
irb(main):001:0> require 'RedCloth'
LoadError: cannot load such file -- 2.2/redcloth_scan
Couldn't load 2.2/redcloth_scan
The $LOAD_PATH was:
C:/Ruby22-x64/lib/ruby/gems/2.2.0/extensions/x64-mingw32/2.2.0/RedCloth- 4.2.9
C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/RedCloth-4.2.9/lib
C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/RedCloth- 4.2.9/lib/case_sensitive_require
C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/RedCloth-4.2.9/ext
C:/Ruby22-x64/lib/ruby/site_ruby/2.2.0
C:/Ruby22-x64/lib/ruby/site_ruby/2.2.0/x64-msvcrt
C:/Ruby22-x64/lib/ruby/site_ruby
C:/Ruby22-x64/lib/ruby/vendor_ruby/2.2.0
C:/Ruby22-x64/lib/ruby/vendor_ruby/2.2.0/x64-msvcrt
C:/Ruby22-x64/lib/ruby/vendor_ruby
C:/Ruby22-x64/lib/ruby/2.2.0
C:/Ruby22-x64/lib/ruby/2.2.0/x64-mingw32
from C:/Ruby22-x64/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54
:in `require'
from C:/Ruby22-x64/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54
:in `require'
from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/RedCloth- 4.2.9/lib/RedCloth.
rb:13:in `<top (required)>'
from C:/Ruby22-x64/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:12
8:in `require'
from C:/Ruby22-x64/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:12
8:in `rescue in require'
from C:/Ruby22-x64/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:39
:in `require'
from (irb):1
from C:/Ruby22-x64/bin/irb:11:in `<main>'
irb(main):002:0>
I don't know if it is the versions of RedCloth that may be doing this? Or some sort of path error? I really am looking for some help here though! Please!
I had to change the path by creating a 2.2 folder to include redcloth_scan.so and I got it to work!
Thanks for the link #Casper

Can't start Mongoid with Sinatra

I'm trying to get started with Mongoid in a Sinatra app, but it looks like I'm running into dependency issues. If it helps, I'm on a Macbook Pro using RVM and running Ruby 1.9.3.
Here are the errors I'm getting after running $ ruby config.ru:
/Users/duncanmalashock/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/rubygems/specification.rb:1637:in `raise_if_conflicts': Unable to activate mongoid-3.0.23, because activemodel-4.0.0 conflicts with activemodel (~> 3.1) (Gem::LoadError)
from /Users/duncanmalashock/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/rubygems/specification.rb:746:in `activate'
from /Users/duncanmalashock/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/rubygems.rb:212:in `rescue in try_activate'
from /Users/duncanmalashock/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/rubygems.rb:209:in `try_activate'
from /Users/duncanmalashock/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/rubygems/custom_require.rb:59:in `rescue in require'
from /Users/duncanmalashock/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/rubygems/custom_require.rb:35:in `require'
from /Users/duncanmalashock/Penumbra/penumbra.rb:4:in `<top (required)>'
from /Users/duncanmalashock/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /Users/duncanmalashock/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from config.ru:2:in `<main>'
Can anyone help?
Here are the files involved:
mongoid.yml:
development:
sessions:
default:
database: mongoid
hosts:
- localhost:27017
config.ru:
require 'sinatra'
require './penumbra'
run Sinatra::Application
Gemfile:
source :rubygems
source :rubyforge
# Thin Server
gem 'thin'
# Sinatra
gem 'sinatra'
gem 'sinatra-contrib', :require => 'sinatra/multi_route'
gem 'sinatra-partial', :require => 'sinatra/partial'
gem 'sinatra-reloader'
# MongoDB
gem "mongoid"
# Shopify
gem 'shopify_api'
penumbra.rb:
require 'sinatra'
require 'shopify_api'
require 'mongo'
require 'mongoid'
require 'json'
require "sinatra/reloader" if development?
Mongoid.load!("./mongoid.yml", :production)
def isactivepage(link_name)
if (link_name == #page_name)
return ' activelink'
else return ''
end
end
get '/' do
#page_name = "Home"
erb :"pages/index"
end
get '/about' do
#page_name = "About"
erb :"pages/about"
end
After running $ bundle exec rackup:
/Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448/gems/mongoid-1.0.6/lib/mongoid.rb:68:in `method_missing': undefined method `load!' for #<Mongoid::Config:0x007fc0838cb1d0> (NoMethodError)
from /Users/duncanmalashock/Penumbra/penumbra.rb:9:in `<top (required)>'
from /Users/duncanmalashock/Penumbra/config.ru:2:in `require'
from /Users/duncanmalashock/Penumbra/config.ru:2:in `block in <main>'
from /Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448/gems/rack-1.5.2/lib/rack/builder.rb:55:in `instance_eval'
from /Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448/gems/rack-1.5.2/lib/rack/builder.rb:55:in `initialize'
from /Users/duncanmalashock/Penumbra/config.ru:in `new'
from /Users/duncanmalashock/Penumbra/config.ru:in `<main>'
from /Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448/gems/rack-1.5.2/lib/rack/builder.rb:49:in `eval'
from /Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448/gems/rack-1.5.2/lib/rack/builder.rb:49:in `new_from_string'
from /Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448/gems/rack-1.5.2/lib/rack/builder.rb:40:in `parse_file'
from /Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448/gems/rack-1.5.2/lib/rack/server.rb:277:in `build_app_and_options_from_config'
from /Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448/gems/rack-1.5.2/lib/rack/server.rb:199:in `app'
from /Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448/gems/rack-1.5.2/lib/rack/server.rb:314:in `wrapped_app'
from /Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448/gems/rack-1.5.2/lib/rack/server.rb:250:in `start'
from /Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448/gems/rack-1.5.2/lib/rack/server.rb:141:in `start'
from /Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448/gems/rack-1.5.2/bin/rackup:4:in `<top (required)>'
from /Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448/bin/rackup:23:in `load'
from /Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448/bin/rackup:23:in `<main>'
You have a Gemfile, but you’re not starting your app using Bundler, and that’s why you have the conflict.
The current version of shopify_api (3.0.3) has a dependency on activemodel via activeresource with the version requirements set to >=3.0.0. On your system you have Rails 4 installed, so activemodel 4.0 is loaded.
The current version of mongoid also has a dependency on activemodel, but this time the version requirement is ~> 3.2.
These two version requirements are not compatible, so you get the error activemodel-4.0.0 conflicts with activemodel (~> 3.1).
To fix it, simply use Bundler to start your app. You say you are running with $ ruby config.ru but this isn’t the correct way to use a config.ru (you would get errors later even if you fixed your dependency issues). You should use rackup. In this case you should run:
$ bundle exec rackup

Ruby version errors / crack/xml in irb

Because of a bug with Float#round in Ruby 1.8.6, I was forced to upgrade to 1.9.3 and boy was that awful. After install, irb didn't work, complaining of a lack of psych. So I tried to install the gem, but it was angry that libyaml didn't exist, so I installed that. Not sure why they weren't included if they were so important.
Now when I use require 'Location.rb' (after having to specify that I actually do want to look in the current folder... using $LOAD_PATH), I get this error:
LoadError: cannot load such file -- crack/xml
from /usr/local/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /usr/local/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /usr/local/lib/ruby/gems/1.9.1/gems/ap-0.1.1/lib/ap.rb:2:in `<top (required)>'
from /usr/local/lib/ruby/1.9.1/rubygems/custom_require.rb:59:in `require'
from /usr/local/lib/ruby/1.9.1/rubygems/custom_require.rb:59:in `rescue in require'
from /usr/local/lib/ruby/1.9.1/rubygems/custom_require.rb:35:in `require'
from /Users/tyre77/Dropbox/Aurora/GMap.rb:4:in `<top (required)>'
from /usr/local/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /usr/local/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from (irb):3
from /usr/local/bin/irb:12:in `<main>'
What does this mean? Also, when I execute ruby -v, it lists my version as 1.9.3p0 but this is dicking around in 1.9.1? All I want is my irb back and working!
It turned out that Ruby was looking for an XML parser called 'crack'. I don't know why it is referencing 2 versions of Ruby or why these dependencies aren't included in the Ruby build (since irb won't work without them) but to fix I installed the crack gem. sudo gem install crack
you can use
require_relative 'Location.rb'
or
require './Location.rb'

Help Getting Started with Mechanize

I'm new to Ruby and just installed Ruby for Windows.
I want to use the mechanize library (https://github.com/tenderlove/mechanize) and so I'm following the guide at https://github.com/tenderlove/mechanize/blob/master/GUIDE.rdoc.
On the Windows cmd line, I installed mechanize by using the cmd "gem install mechanize".
When I run the following code:
require 'rubygems'
require 'mechanize'
agent = Mechanize.new
I get the error:
<internal:lib/rubygems/custom_require>:29:in `require': no such file to load -- net/http/digest_auth (LoadError)
from <internal:lib/rubygems/custom_require>:29:in `require'
from C:/Ruby192/lib/ruby/1.9.1/mechanize.rb:5:in `<top (required)>'
from <internal:lib/rubygems/custom_require>:29:in `require'
from <internal:lib/rubygems/custom_require>:29:in `require'
from helloworld.rb:2:in `<main>'
Anybody know what's going on?
It seems that some dependencies are missing. Try to install the net-http-digest_auth gem.
gem install net-http-digest_auth
If that solves this problem and another (related) pops up, it's probable that you are missing the net-http-persistent gem. If that's the case, you know what to do! Just install it too.

Resources