I have modular style sinatra app, with the following line near the end, so that it can be run standalone:
# ... all code before this omitted
run! if __FILE__ == $0
end
# This is the end of the file
When I run this app with ruby app.rb it works fine, and webrick starts up.
However, if I run it instead with bundle exec ruby app.rb I get this error:
>bundle exec ruby app.rb
C:/Ruby200/lib/ruby/gems/2.0.0/gems/sinatra-1.4.4/lib/sinatra/base.rb:1488:in `start_server': undefined method `run' for HTTP:Module (NoMethodError)
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/sinatra-1.4.4/lib/sinatra/base.rb:1426:in `run!'
What's causing this error?
Explicitly set your webserver, e.g.
set :server, 'thin'
and make sure you add whatever server you’re using to your Gemfile, e.g.
gem 'thin'
Related
I have a sinatra app with a Rakefile.rake and server.rb file. In the server.rb file I have
get '/' do
rake :test
end
the app loads up but crashes when I load localhost:4567/ and says undefined method 'rake' for sinatra application. I try and require the file by using
require '/home/user/project/Rakefile'
and I also try Rakefile.rake but both give me an error that reads "require cannot load such file." I'm using Ubuntu.
I'm not sure why sinatra can't load the rakefile, but when I run rake test in terminal that works.
Any help would be great.
get '/' do
system 'rake test'
end
I'm executing the file app.rb in my rails project and trying to step through it using the byebug gem, but I get an error saying 'byebug' is an undefined local variable. I'm running the code using the command 'ruby app.rb'. Is there a different way to step through a ruby file when executing it this way via the command line?
$ bundle install
Fetching gem metadata from https://rubygems.org/.....
Resolving dependencies...
Using byebug 6.0.2
Installing pg 0.18.3
Using bundler 1.6.2
Your bundle is complete!
Use `bundle show [gemname]` to see where a bundled gem is installed.
$ ruby app.rb
app.rb:17:in `block in <main>': undefined local variable or method `byebug' for main:Object (NameError)
from app.rb:15:in `glob'
from app.rb:15:in `<main>'
Looks like I just had to add require "byebug" at the top my app.rb file.
Another debugging option is the debugger gem:
require 'debugger'; debugger
https://github.com/cldwalker/debugger
I have a ruby extension written in C++, P4, and it seems to generally work:
I can run irb -Ilib and then require 'P4', and use it
I can execute tests via rake by accessing the shell script in the bin folder of the rake gem, e.g., ${GEM_HOME}/gems/rake-10.3.2/bin/rake test
however, when I access rake via the RubyGems wrapper in my path, e.g., rake test, I get this TypeError
/Users/tjuricek/dev/p4ruby/lib/P4.rb:38:in `require': P4 is not a class (TypeError)
from /Users/tjuricek/dev/p4ruby/lib/P4.rb:38:in `<top (required)>'
from /Users/tjuricek/dev/p4ruby/test/testlib.rb:31:in `require'
from /Users/tjuricek/dev/p4ruby/test/testlib.rb:31:in `<top (required)>'
from /Users/tjuricek/.rvm/gems/ruby-2.1.2/gems/rake-10.3.2/lib/rake/rake_test_loader.rb:15:in `require'
from /Users/tjuricek/.rvm/gems/ruby-2.1.2/gems/rake-10.3.2/lib/rake/rake_test_loader.rb:15:in `block in <main>'
from /Users/tjuricek/.rvm/gems/ruby-2.1.2/gems/rake-10.3.2/lib/rake/rake_test_loader.rb:4:in `select'
from /Users/tjuricek/.rvm/gems/ruby-2.1.2/gems/rake-10.3.2/lib/rake/rake_test_loader.rb:4:in `<main>'
rake aborted!
Then it pops out the ruby command that "failed". If I copy and paste that command and run it, it works.
What I've noticed is that RubyGems creates a fairly simple wrapper script:
#!/usr/bin/env ruby_executable_hooks
#
# This file was generated by RubyGems.
#
# The application 'rake' is installed as part of a gem, and
# this file is here to facilitate running it.
#
require 'rubygems'
version = ">= 0"
if ARGV.first
str = ARGV.first
str = str.dup.force_encoding("BINARY") if str.respond_to? :force_encoding
if str =~ /\A_(.*)_\z/ and Gem::Version.correct?($1) then
version = $1
ARGV.shift
end
end
gem 'rake', version
load Gem.bin_path('rake', 'rake', version)
I'm guessing that the last line, load Gem.bin_path... has triggered some kind of misconfiguration of my part in creating my extension, but I have no idea what that would be.
Does anyone have ideas on what might cause require to fail only when run under the RubyGems wrapper?
OK, the way to debug this is to go into a line where you load the file:
require 'P4.so'
And see if it's defined:
puts "P4 #{P4.class}"
require 'P4.so'
In this case, when running under rake directly, it loaded the .gemspec which pulled in a version definition that created (incorrectly, in my case) a P4 module:
module P4
version = VERSION = '3000.0.0.pre0'
end
So, for me the fix included:
Changing module P4 to class P4
Requiring the version definition before my require 'P4.so' statement, typically: require_relative 'P4/version'
Not defining the class in my C++ extension code, but loading it and extending the one defined in my version file:
// Ensure the class has been defined by the version specification
cP4 = rb_path2class("P4");
I'm having trouble learning how to use Sinatra. I finally got phusion-passenger installed and working with my apache2 on Ubuntu. I have the following directories and files
/var/www/html
/var/www/html/public
/var/www/html/tmp
/var/www/html/config.ru # this is a file
The contents of /var/www/html/config.ru is copied from https://www.phusionpassenger.com/documentation/Users%20guide%20Apache.html#_tutorial_example_writing_and_deploying_a_hello_world_rack_application.
When I start up this application, I get hello world, which is great.
So next, I want to build a Sinatra app. I went ahead and created the file
/var/www/html/myapp.rb
With the contents described by http://www.sinatrarb.com/intro.html . I also did a gem install sinatra. I restarted apache. Then I went to http://localhost/ but I still see hello world of my config.ru. So then I overwrote the contents of config.ru with myapp.rb. I restarted apache. But now I get an error message
missing run or map statement (RuntimeError)
/usr/lib/ruby/vendor_ruby/rack/builder.rb:133:in `to_app'
config.ru:1:in `<main>'
/usr/share/passenger/helper-scripts/rack-preloader.rb:112:in `eval'
/usr/share/passenger/helper-scripts/rack-preloader.rb:112:in `preload_app'
/usr/share/passenger/helper-scripts/rack-preloader.rb:158:in `<module:App>'
/usr/share/passenger/helper-scripts/rack-preloader.rb:29:in `<module:PhusionPassenger>'
/usr/share/passenger/helper-scripts/rack-preloader.rb:28:in `<main>'
What am I doing wrong? How do I build a hello world Sinatra app?
Your config.ru should require and run your app, like:
require './myapp.rb'
run Sinatra::Application
And in turn, myapp should require sinatra:
require 'sinatra'
get '/' do
'Hello world!'
end
Read on about using a config.ru: http://www.sinatrarb.com/intro.html#Using%20a%20Classic%20Style%20Application%20with%20a%20config.ru
I'm using:
Rails 3.0.7 and
Rspec 2.5.0
via rvm
When I run this spec (using autotest or bundle exec autotest or bundle exec rspec spec/) below:
require 'spec_helper'
require 'yaml'
def twitter_feed(id=1)
ruby_object = YAML.load_file(::Rails.root.to_s + "/spec/fixtures/feeds/twitter_response_#{id}.yml")
end
I get this:
Failure/Error: ruby_object = YAML.load_file(::Rails.root.to_s + "/spec/fixtures/feeds/twitter_response_#{id}.yml")
TypeError:
invalid subclass
# ./spec/models/tweet_spec.rb:6:in `twitter_feed'
# /Users/natebean/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/yaml.rb:133:in `transfer'
# /Users/natebean/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/yaml.rb:133:in `node_import'
# /Users/natebean/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/yaml.rb:133:in `load'
# /Users/natebean/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/yaml.rb:133:in `load'
# /Users/natebean/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/yaml.rb:144:in `load_file'
# /Users/natebean/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/yaml.rb:143:in `open'
# /Users/natebean/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/yaml.rb:143:in `load_file'
# ./spec/models/tweet_spec.rb:5:in `twitter_feed'
# ./spec/models/tweet_spec.rb:58
This "was" working. I can't find any other information on this error on the internet. I've moved from rails 3.0.3 to 3.0.7, but don't remember it not working after the upgrade.
Any suggestions? Thanks.
The yaml file I was pulling was looking for Hashie::Mash to map the data to. Up to know I didn't need to require 'hashie', but that has "fixed this problem".
I added to this to my spec and it is now working.
require 'hashie'
Run bundle exec rspec spec --backtrace to get a full backtrace so you can see exactly where that error is coming from.