Why won't Sinatra work correctly?
I was recently trying to set up an Amazon EC2 instance to run Rails and Sinatra apps. The problem - Sinatra won't start. When I try to run the .rb file, it throws something like this:
$ ruby hello.rb
/usr/local/rvm/gems/ruby-1.8.7-p334/gems/sinatra-1.2.5/lib/sinatra/base.rb:1144:in `define_method': tried to create Proc object without a block (ArgumentError)
from /usr/local/rvm/gems/ruby-1.8.7-p334/gems/sinatra-1.2.5/lib/sinatra/base.rb:1144:in `compile!'
from /usr/local/rvm/gems/ruby-1.8.7-p334/gems/sinatra-1.2.5/lib/sinatra/base.rb:1129:in `route'
from /usr/local/rvm/gems/ruby-1.8.7-p334/gems/sinatra-1.2.5/lib/sinatra/base.rb:1111:in `get'
from /usr/local/rvm/gems/ruby-1.8.7-p334/gems/sinatra-1.2.5/lib/sinatra/base.rb:1474:in `send'
from /us开发者_C百科r/local/rvm/gems/ruby-1.8.7-p334/gems/sinatra-1.2.5/lib/sinatra/base.rb:1474:in `get'
from hello.rb:4
Now, I initially thought that was something specific to EC2. So I set up RVM on my Mac and tried the same - same result. Then I thought it might be a specific version of Ruby being used (1.9.2). It is not - the problem persists even with 1.8.7. Now, I am completely lost. Here is the list of gems currently installed on my EC2 instance:
$ gem list
*** LOCAL GEMS ***
abstract (1.0.0)
actionmailer (3.0.7)
actionpack (3.0.7)
activemodel (3.0.7)
activerecord (3.0.7)
activeresource (3.0.7)
activesupport (3.0.7)
arel (2.0.9)
builder (2.1.2)
bundler (1.0.12)
coderay (0.9.7)
erubis (2.6.6)
i18n (0.5.0)
mail (2.2.19)
method_source (0.4.1)
mime-types (1.16)
polyglot (0.3.1)
pry (0.8.3)
rack (1.2.2)
rack-mount (0.6.14)
rack-test (0.5.7)
rails (3.0.7)
railties (3.0.7)
rainbow (1.1.1)
rake (0.8.7)
ruby_parser (2.0.6)
sexp_processor (3.0.5)
sinatra (1.2.5)
slop (1.5.3)
thor (0.14.6)
tilt (1.3)
treetop (1.4.9)
tzinfo (0.3.27)
Please let me know what you think of that - any help is appreciated.
Looks like it's a bug in sinatra: https://github.com/sinatra/sinatra/issues/258.
As a workaround, try the previous version of sinatra: gem install sinatra -v 1.2.3
, either in a new rvm gemset, or specify the version you want in your file with gem 'sinatra' '=1.2.3'
before your require sinatra
line.
Update:
Sinatra 1.2.5 (the version at fault) has been yanked and a new version released. Anyone getting this error can now just do gem update sinatra
and use the updated gem.
It's not a RVM problem, as I use it with Sinatra.
Here's some test code:
#!/usr/bin/env ruby
require 'sinatra'
get '/hi' do
"Hello World! #{ `rvm -v` }"
end
Save that to something like test.rb
and do a chmod +x test.rb
to make it executable. Confirm you're running Ruby under RVM's control using which ruby
, which should return a path in your ~/.rvm
path. Run the code using ./test.rb
and your RVM controlled Ruby will start it.
The console out:
>> == Sinatra/1.2.3 has taken the stage on 4567 for development with backup from WEBrick
>> [2011-04-30 14:49:56] INFO WEBrick 1.3.1
>> [2011-04-30 14:49:56] INFO ruby 1.9.2 (2011-02-18) [x86_64-darwin10.6.0]
>> [2011-04-30 14:49:56] INFO WEBrick::HTTPServer#start: pid=3387 port=4567
>> 127.0.0.1 - - [30/Apr/2011 14:50:02] "GET /hi HTTP/1.1" 200 103 0.2499
>> localhost - - [30/Apr/2011:14:50:01 MST] "GET /hi HTTP/1.1" 200 103
>> - -> /hi
>> 127.0.0.1 - - [30/Apr/2011 14:50:02] "GET /favicon.ico HTTP/1.1" 404 441 0.0008
>> localhost - - [30/Apr/2011:14:50:02 MST] "GET /favicon.ico HTTP/1.1" 404 441
>> http://localhost:4567/hi -> /favicon.ico
>>
>> == Sinatra has ended his set (crowd applauds)
>> [2011-04-30 14:50:11] INFO going to shutdown ...
>> [2011-04-30 14:50:11] INFO WEBrick::HTTPServer#start done.
And what I saw in my browser:
Hello World! rvm 1.6.5 by Wayne E. Seguin (wayneeseguin@gmail.com) [https://rvm.beginrescueend.com/]
Changing it to:
get '/hi' do
"<pre>#{ `which ruby` }</pre>"
end
displays this in my browser:
/Users/greg/.rvm/rubies/ruby-1.9.2-p180/bin/ruby
So, basically, I've got RVM running on my system and Ruby 1.9.2 is the current Ruby. Try that code, run it and see if you get output in your browser.
Also see whether your RVM is current. It bumped up to 1.6.5 last week. If you're not current use:
rvm get head
rvm reload
EDIT:
One further test to show which Ruby is running: Change the code to this:
#!/usr/bin/env ruby
require 'sinatra'
get '/hi' do
"Running using: #{RUBY_VERSION}<br>Ruby in path found at: #{ `which ruby` }"
end
and run it. If your interpreter is running the system-installed Ruby on MacOS or the default install for Ruby via apt-get, you'll be on 1.8.7. I get:
Running using: 1.9.2
Ruby in path found at: /Users/greg/.rvm/rubies/ruby-1.9.2-p180/bin/ruby
精彩评论