开发者

Sinatra doesn't load when running a ruby app

开发者 https://www.devze.com 2023-02-28 13:27 出处:网络
I\'m trying to run a Ruby application that requires Sinatra within Ubuntu 10.10. I\'m new to the 3 of these technologies so I understand if this question looks dumb to you.

I'm trying to run a Ruby application that requires Sinatra within Ubuntu 10.10. I'm new to the 3 of these technologies so I understand if this question looks dumb to you.

Yesterday I installed ruby doing... sudo apt-get install ruby1.9.1-full

And sinatra by doing... sudo gem inst开发者_如何学运维all sinatra

This is the code I'm trying to run:

require 'rubygems'
require 'sinatra'

get '/' do
    "Hi Alex!"
end

When I do ruby1.9.1 -rubygems app.rb nothing happens (Ruby is properly installed since I tried running apps that don't require sinatra and they work OK).

$ ls
app.rb
$ 
$ ruby1.9.1 app.rb
$
$ ruby1.9.1 -rubygems app.rb
$ 

I know it should open Sinatra and tell me which port it is listening to.

I've been looking for help through the web and read several of the threads created within this forum but nothing I've tried has worked out for me.

What could be happening here?

Thanks


There was a similar problem with sinatra 1.0 on ruby 1.9.2. The answer there was to add enable :run to your code.

Have a look at the docs for the :run configuration - if you're going to be deploying to a server you'll want to do something like enable :run if __FILE__ == $0 so that you only start the built-in server during development when you need it.

Strictly speaking your code is correct and should run okay, and in fact it does with ruby 1.8.7 and 1.9.2. The problem seems to be running it with ruby 1.9.1. In general 1.9.1 seems to be fairly outdated and you should probably look to upgrade to 1.9.2 if you can. If Ubuntu doesn't have any packages for 1.9.2 take a look at rvm. (In fact if you're going to be doing ruby development rvm is worth a look anyway).


Update:

I managed to get ruby 1.9.1 compiled to test this, and your code worked ok. Also a bit of googling suggests that the Ubuntu ruby1.9.1 package provides ruby 1.9.2 anyway. So there seems to be something else going on causing :run not to be set when running the file directly, though I don't know what that could be.

Another update:

Looking at the Ubuntu Sinatra package it looks like it's at version 1.0. It could be that your setup is using the Ubuntu package and ignoring the more recent version installed via rubygems. This could explain what's happening. If so this isn't a "a similar problem" to ruby 1.9 and sinatra 1.0 like I suggested above, it's the same problem!

Yet another update:

A couple of things have occurred to me. You can check what version of Sinatra you're actually using with something like puts Sinatra::VERSION after require 'sinatra'. Also, it looks like there is a gem1.9.1 command that corresponds to ruby1.9.1. It looks like when you installed sinatra with sudo gem install sinatra the latest version got installed into the ruby 1.8 install, and left ruby 1.9 with the Ubuntu packaged Sinatra 1.0. If you haven't switched over to rvm yet, you could try sudo gem1.9.1 install sinatra.

0

精彩评论

暂无评论...
验证码 换一张
取 消