I'm trying to use httparty
require 'httparty'
but I'm getting
no such file to load -- httparty (LoadError)
I'm using Ruby 1.8.7 on OSX
On Ruby 1.8, you have to do require "rubygems" before you can require any Gems in your code. So:
require "rubygems"
require "httparty"
# Your code here.
Related
When I require 'active_support/core_ext', then get a error :
NameError: uninitialized constant ActiveSupport::Autoload from /opt/rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/number_helper.rb:3:in `<module:NumberHelper>'
Of course I installed activesupport gem.
# gem list --local | grep activesupport
activesupport (4.2.0)
Should I install some other gems to use active_support/core_ext?
I'm using ruby 2.1.5p273 in Ubuntu14.04.
Presuming you're using bundler, try this diagnostic code to see what works:
require 'rubygems' # You may be able to omit this line
require 'bundler/setup' # You may be able to omit this line
require 'active_support'
require 'active_support/core_ext'
Newer Ruby versions may be able skip rubygems, setup, and core_ext, and just use this:
require 'active_support'
I am sure this is a simple issue but since I have looked so long I cannot see it. So I am running ruby 1.9.3 with Sinatra, sqlite3, datamapper, dm-sqlite-adapter. When I try to run Sinatra, I get this:
/Users/XXX/.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 -- datamapper (LoadError)
from /Users/XXX/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from app.rb:2:in `<main>'
Here are the first two lines:
require 'sinatra'
require 'datamapper'
The gem is installed. (datamapper (1.2.0))
You need to require data_mapper, not datamapper (note the underscore):
require 'data_mapper'
See the DataMapper getting started page.
When I try to require the tagfile/tagfile which is part of rtaglib I get a LoadError:
$ ruby main.rb
/usr/share/rubygems/rubygems/custom_require.rb:36:in `require': cannot load such file -- tagfile/tagfile (LoadError)
from /usr/share/rubygems/rubygems/custom_require.rb:36:in `require'
from main.rb:4:in `<main>'
I installed rtaglib with
$ gem install rtaglib
Here is the top part of my main.rb:
require 'date'
require 'find'
require 'tagfile/tagfile'
None of the suggestions here (Problem with Ruby + rtaglib gem) work. taglib is installed (1.7.2)
Trying it with other gems, like sinatra, works perfectly. Does anyone know the reason I cannot load rtaglib?
Can't you just require 'tagfile'? I just tried it and it worked fine.
Gemfile
source :rubygems
gem 'sinatra'
config.ru
require 'app'
run App
app.rb
require 'bundler/setup'
require 'sinatra'
class App < Sinatra::Base
get '/' do
'hello world'
end
end
rackup failes with
.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': no such file to load -- app (LoadError)
Works with ruby 1.8 . Why?
I think it's because 1.9.2 no longer includes '.' in the load path by default.
See the this question for more: Why does Ruby 1.9.2 remove "." from LOAD_PATH, and what's the alternative?
Some notes:
Gemfile, I use gem 'sinatra', :require => 'sinatra/base' to load a Modular Sinatra App.
Config.ru, usually I set Bundler on it, not in app.rb, leaving app.rb clean to my app.
require 'bundler/setup'
Bundler.require(:default)
I'm trying to get Bundler setup so I can deploy my Sinatra app to server with all the correct gems.
I've created my Gemfile
source :gemcutter
gem 'sinatra', '1.0'
gem "nokogiri", "1.4.2"
gem "rack", "1.1.0"
gem "dm-core", "1.0.0"
gem "dm-migrations", "1.0.0"
gem "dm-sqlite-adapter", "1.0.0"
gem "pony", "1.0"
Next I created a Config.ru
require 'rubygems'
require 'bundler'
Bundler.setup
require 'sinatra'
require 'dm-core'
require 'dm-migrations'
require 'dm-sqlite-adapter'
require 'open-uri'
require 'nokogiri'
require 'csv'
require 'pony'
require 'parsedate'
require 'digest/md5'
require 'MyApp'
run MyApp
So far so good, so next I ran bundle install and got 'Bundle Complete' so now all I need to do is just Rackup
Then I get:
config.ru:18: undefined local variable or method `MyApp' for #<Rack::Builder:0x1227350 #ins=[]> (NameError)
from /usr/local/lib/ruby/gems/1.8/gems/rack-1.1.0/lib/rack/builder.rb:46:in `instance_eval'
from /usr/local/lib/ruby/gems/1.8/gems/rack-1.1.0/lib/rack/builder.rb:46:in `initialize'
from config.ru:1:in `new'
from config.ru:1
Here's a simple MyApp.rb which will trigger the same error
get '/' do
erb :index
end
What's going wrong? :(
If you tell Rack to run MyApp, you need to define class MyApp first (which you're supposed to do inside MyApp.rb). Derive your class from Sinatra::Base to make it a Sinatra-Rack-App that can be run from config.ru:
require 'sinatra/base'
class MyApp < Sinatra::Base
get '/' do
erb :index
end
end
See also Sinatra's README about modular Sinatra apps (search for the paragraph named "Modular Apps" on http://github.com/sinatra/sinatra/)
Additionally you may have your my_app.rb as follows:
require 'rubygems'
require 'bundler'
Bundler.setup
require 'sinatra'
require 'dm-core'
require 'dm-migrations'
require 'dm-sqlite-adapter'
require 'open-uri'
require 'nokogiri'
require 'csv'
require 'pony'
require 'parsedate'
require 'digest/md5'
And your config.ru like this:
require './my_app'
run Rack::URLMap.new '/' => Sinatra::Application
Hope this helps.
Best Regards
ED
As an alternative to creating a modular app (wrapping your Sinatra methods in a class extending Sinatra::Base), you can use:
run Sinatra::Application
in the config.ru file in place of
run MyApp
This might be a better option if you want to keep the simple Sinatra code.
See the docs for more info.