Sinatra doesn't load when running a ruby app - ruby

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 install 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.

Related

Ruby Gems suddenly stopped working on ubuntu 12.04

After a couple of days of not doing pretty much anything on the ubuntu box, I decided to try out some ruby stuff. For this, I wanted to fire up pry. Unfortunately, I was presented with
Sorry, you can't use Pry without Readline or a compatible library.
Please gem install rb-readline or recompile Ruby --with-readline.
~/.rbenv/versions/2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in
`require': libreadline.so.5: cannot open shared object file: No such
file or directory -
~/.rbenv/versions/2.1.1/lib/ruby/2.1.0/x86_64-linux/readline.so
(LoadError)
Being quite new to linux in general, I figured I'd just do as it said, and install this rb-readline. The installation passes just fine, but doesnt seem to do anything at all. If I do gem list p, the package just doesn't appear.
So yeah, I just don't know what to do to get it working again, and the only thing I managed to find by searching was related to an installation without rbenv and had something to do with sudo etc.
Any ideas?
Without understanding the problem and following the advice in option 2 of the message:
Sorry, you can't use Pry without Readline or a compatible library.
Possible solutions:
* Rebuild Ruby with Readline support using `--with-readline`
* Use the rb-readline gem, which is a pure-Ruby port of Readline <==== Option 2
* Use the pry-coolline gem, a pure-ruby alternative to Readline
I added the gem into the Gemfile (as follows), bundled and pry was then available.
group :development, :test do
gem 'pry'
gem 'rb-readline'
end
I had this problem too. I am using rbenv and reinstalling ruby via
rbenv install -f 2.2.3
did fixed it for me. Of course you'd put in your respective version. -f forces the installation even though you already have that specific version installed. use rbenv global to find out what version you have installed and set.
I have some problem. But I don't want to add gem 'rb-readline'. So try it...
$ln -s /usr/local/opt/readline/lib/libreadline.8.0.dylib /usr/local/opt/readline/lib/libreadline.7.dylib
Do a sudo apt-get install libreadline-dev, seems like you're missing the readline shared library that pry is wanting. If it worked before, not sure why the library isn't there anymore.
I ended up doing rbenv uninstall, followed by rbenv install and re-installing all the gems, and got it back to working. Still no idea what caused it in the first place, but it works now.

How can I restart Ruby after a gem install?

I'm running on Ubuntu. The question is so basic it feels silly but I have done some searching and can't figure out the answer.
Edit: I am running into similar issues as the following
Uninitialized constant using Twitter ruby gem
You don't need to restart Ruby after installing gems. You can for example
gem install rails
and after that you can start running rails (which uses Ruby) right away and no need to restart Ruby.
In Uninitialized constant using Twitter ruby gem link restart means ctr+c that is stop the server and rails s (for rails 3) that is start the server.

Ruby Twitter gem

-- UPDATE --
Ok its fixed. This is what I did. remove all ruby and rubygems completely. then install ruby1.9.1-full and rubygems1.9.1 then install the twitter gem.
Hi guys,
I am having trouble working with the Twitter gem. I am using ruby 1.8.7
After installing when I try to run a simple script I get this error
ruby twitter.rb
./twitter.rb:5: uninitialized constant Twitter (NameError)
from /usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in gem_original_require'
from /usr/lib/ruby/1.8/rubygems/custom_require.rb:31:inrequire'
from twitter.rb:2
I running this on a Ubuntu box. I checked with gem -list and I see the Twitter (1.1.0) is listed there.
this is the code I am trying to run
require "rubygems"
require 'twitter'
puts Twitter.user_timeline("test").first.text
Any ideas ?
I believe it only works with Ruby 1.9 If you want to use twitter gem try version 0.9 with Ruby 1.8.x
This works for me:
ruby-1.9.2-p0 > require 'twitter'
=> true
ruby-1.9.2-p0 > puts Twitter.user_timeline("test").first.text
TExES Pedagogy and Professional Responsibilities EC-12 Teacher Certification Test Prep Study Guid… - by Sharon A Wynne http://amzn.to/f3kF74
=> nil
which version of ruby are you using?
gouravtiwari21's comment seems to fix the problem, but it's wrong to suggest that the twitter gem requires 0.9.0 if you want to run it using Ruby 1.8.x.
You can check out the version compatibility here:
http://travis-ci.org/#!/jnunemaker/twitter
It shows the twitter gem working with Ruby installs as low as 1.8.7.
For me, it was an issue with having the correct dependent gems, as well as the correct versions.
Here's how I got it working:
I ran:
sudo gem list
And compared the versions of specific gems with what I found here:
https://github.com/jnunemaker/twitter/blob/master/HISTORY.md (I simply searched for the word 'dependency' to see which versions twitter cared about.
I also found this diff:
https://github.com/jnunemaker/twitter/commit/ac8114c1f6ba2da20c2267d3133252c2ffc6b6a3
And I compared the gems listed there with what I had installed, and I just made sure my system lined up with what I was seeing in the version notes. Oftentimes what happened is that I had multiple versions of a gem, and for some reason, the lower version was taking precedence.
I'm not sure why I still have to add
gem 'twitter', '1.7.1'
to my Gemfile, but alas, that's the last step required in order to get this stuff working.
Don't forget to restart your server, and you should be good!

Why is Phusion Passenger refusing to recognize my updated RubyGems?

I've updated RubyGems everywhere I can possibly think to update it:
The various places I've updated RubyGems http://files.droplr.com/files/14167865/zel8k.Screen%20shot%202010-03-09%20at%2017:24:59.png
but Phusion passenger keeps throwing the same error:
Error Screenshot http://files.droplr.com/files/14167865/zee8W.Screen%20shot%202010-03-09%20at%2017:19:50.png
There is that one freaking place that shows 1.3.1 as the version, despite the file name of 1.3.6. I've obviously borked things up pretty badly here.
That is, I need to update to 1.3.2 or greater and that I currently have 1.3.1.
Any idea how in the world I can get PP to use the correct version of RubyGems and preferably change things so I don't have to face this problem again?
Could this have anything to do with different paths for RubyGems and Ruby environments? These are my environments:
Environment screenshots http://files.droplr.com/files/14167865/zfH6x.Screen%20shot%202010-03-09%20at%2019:07:18.png
First, obtain the value of your PassengerRuby configuration. Then use that Ruby interpreter to run the following script:
http://pastie.org/862871
Like this:
/path-to-your-passenger-ruby-interpreter myscript.rb
and follow the instructions.
That might be of several reasons.
First, verify that you don't have it to explicitly require the rubygem in environment.rb. That is, if you have a line like this:
config.gem "ruby_gems", :version => "1.3.1"
Then you'll have to remove the version from it.
Also, seeing that the rubygems-update gives the incorrect version, you might want to uninstall it.
sudo gem uninstall rubygems-update
Since you have updated it, that gem is not necessary anymore.
I don't think it will help, but I would probably uninstall the macports version of rubygem as well since that might create collisions, even though it doesn't seem like it in this case.
Update:
Since you are using the Ruby from the operating system you don't have to install rubygem through macports at all.
To remove it:
sudo port uninstall rubygems
After that, make sure that you have upgraded the correct rubygem (the binary shows correct, but perhaps there is something missing)
sudo gem update --system
After you've done that. Open up irb (that is, irb and not the rails console) and check the following:
require 'rubygems'
puts Gem::RubyGemsVersion
Also, if you could paste how your apache/nginx configuration looks like, I doubt there is anything wrong in there, but you never know.

Why can't Shoes::Setup#gem find my gem?

Despite reading what has been written on this here and here and here, I still can't get these lines to work for me:
Shoes.setup do
gem 'statemachine'
end
require 'rubygems'
require 'statemachine'
I have run "sudo gem install statemachine" successfully and copied the gem by hand to ~/.shoes/ to no avail so far.
I am using ruby 1.8 under Mac OS X 10.5.8 and the gem I'm trying to use has no dependencies and works perfectly when invoked from Ruby.
I am trying to grok the Shoes and Gem code to solve this mystery but could use some help from experienced programmers.
Thanks.
That code works fine for me. Shoes should go out on its own and get the gem if needed. Your problem may be that you copied the gem over. Try deleting it from ~/.shoes/ and running the code again.

Resources