Segmentation Fault in rvm'd Ruby on Mac when running RSpec - ruby

I was developing something at the uni, saved to my Dropbox intending to continue at home. This is the message that greeted me:
$ spec graph_spec.rb
/Users/amadan/.rvm/gems/ruby-1.9.2-rc1/gems/PriorityQueue-0.1.2/ext/priority_queue/CPriorityQueue.bundle: [BUG] Segmentation fault
ruby 1.8.7 (2009-06-12 patchlevel 174) [universal-darwin10.0]
However,
$ `which spec` graph_spec.rb
...........................................................................
Finished in 0.046973 seconds
75 examples, 0 failures
What the heck is going on here?
For the reference:
$ which spec
/Users/amadan/.rvm/gems/ruby-1.9.2-rc1/bin/spec
UPDATE: I just noticed the 1.8.7 there... how did it get there? The top of the spec file says:
$ head `which spec`
#!/Users/amadan/.rvm/rubies/ruby-1.9.2-rc1/bin/ruby
#
# This file was generated by RubyGems.
#
# The application 'rspec' is installed as part of a gem, and
# this file is here to facilitate running it.
#
require 'rubygems'
Where does it say "run 1.8.7"?!?

It's likely that RVM is messing your gems and rubies. I would recommend testing on a cleaned up RVM installation (with only 1.9 installed).
Is /Users/amadan/.rvm/rubies/ruby-1.9.2-rc1/bin/rubyreally ruby 1.9.2 ?
Other way to test would be to explicitely run ruby spec so you are sure this is really 1.9.2 which is called.
To conclude, Segfaults do happen in ruby (esp. on 1.8) and are sometimes avoided by reorganizing slightly the ruby code. Good Luck !

Related

Nokogiri Ruby 'require' Issues

I'm new to Ruby and I'm having a lot of trouble trying to use Nokogiri. I've been trying to find a resolution for hours now, so any help is appreciated. I tried searching for and using solutions from other related SO posts before caving and posting my own. When I run ruby -v I get: ruby 1.8.7 (2011-06-30 patchlevel 352) [x86_64-linux]
(Edit: I have updated ruby with updates-alternatives --config ruby and selected /usr/bin/ruby1.9.1 but when I do ruby -v it is now showing version 1.9.3 WTF am I doing wrong here?)
I have a new project directory at ~/workspace/ruby/rubycrawler/ and I used Bundler to install nokogiri, which installed correctly:
Using mini_portile (0.5.2)
Using nokogiri (1.6.1)
Using bundler (1.5.1)
Your bundle is complete!
Running bundle show nokogiri returns /var/lib/gems/1.9.1/gems/nokogiri-1.6.1.
In the directory I'm running the script from I have a simple html file named "index.html". The script I'm trying to run is even simpler (or so I thought):
require 'nokogiri'
page = Nokogiri::HTML(open("index.html"))
puts page.class # Nokogiri::HTML::Document
The error is rubycrawler.rb:1:in 'require': no such file to load -- nokogiri (LoadError).
I also added require 'rubygems' even though I read it isn't needed for 1.9+ and still no luck.
A lot of searching shows "Did you put this gem in your Gemfile?". So I generate a Gemfile and add gem 'nokogiri'. I try running the small script again and get the same error. I read "Try deleting Gemfile.lock." so I did but still couldn't get it to work. I then read to try testing it out in irb so I tested "open-uri" and "nokogiri" and here's what I got:
irb(main):001:0> require 'open-uri'
=> true
irb(main):003:0> require 'nokogiri'
LoadError: no such file to load -- nokogiri
I'm really having a lot of trouble figuring this out, so really any help at all is really appreciated.
Ruby tools like RVM, Bundler, etc., to the novice, appear to do a lot of magic, but really, there is no magic to them. The key here lies in what Bundler actually does for you. It manages a manifest of dependencies, BUT at runtime, those dependencies STILL have to get loaded somehow, and my gut feeling is that is what is not happening here.
Regardless of what version of Ruby you are using, if you are using Bundler, there's an easy way to do this. Precede the command that starts your program with "bundle exec" and that will make Bundler edit Ruby's load path so that it includes all the things in the manifest (Gemfile.lock).
For example:
$ bundle exec ruby foo.rb
A additional note for anyone using RVM: RVM generally will modify the shebangs in the scripts that launch programs like "ruby" or "rake" so that they use the "ruby_no_exec" shell (or similar) instead of the plain old "ruby" shell. That alternate shell is Bundler-aware and makes it generally unnecessary to type "bundle exec," but since the OP is using system Ruby, that's not applicable and commands should be manually prefixed with "bundle exec".
Hope this helps!
In addition to Kent's answer, I would recommend switching to RVM instead of using the system installed ruby. System rubies tend to be horribly out of date, especially when it comes to important things like features and security updates. It might not help you in your current situation, but it would be well worth the time. If you are unfamiliar: http://rvm.io

Call Ruby 1.8 script from Ruby 2.0 script

I'm not sure if this belongs here or somewhere else (SuperUser?) but anyway:
I've got two Ruby scripts, one which requires Ruby 2.0 (A) and another which requires 1.8 (B). A needs to call B with a forked processes. A is something like this:
require "fileutils"
require "json"
...
`name_of_B`
B is an executable script with a shebang, starting like this:
#!/Users/user_name/.rvm/rubies/ruby-1.8.7-p374/bin/ruby
require 'rubygems'
require 'json'
...
I use RVM to manage my Ruby versions:
> rvm list
rvm rubies
ruby-1.8.7-p374 [ i686 ]
ruby-1.9.3-p448 [ x86_64 ]
=* ruby-2.0.0-p247 [ x86_64 ]
I run A with:
> ruby name_of_A
but end up with:
/Users/jacobevelyn/.rvm/gems/ruby-2.0.0-p247/gems/json-1.8.1/lib/json/ext/parser.bundle: [BUG] Segmentation fault
ruby 1.8.7 (2013-06-27 patchlevel 374) [i686-darwin12.5.0]
Any thoughts on what I can do? I don't know a whole lot about gems but it appears that B tries to look at gems installed under Ruby 2.0, rather than 1.8. (Yes, I've run gem install json under 1.8 already.) Obviously the scripts are more complicated than they appear here and absolutely cannot be ported or combined (this doesn't mean I don't want to, it means I can't for my use case), otherwise I would.
you need to change the shebang to:
#!/Users/user_name/.rvm/wrappers/ruby-1.8.7-p374/ruby
it will not only use that ruby but also its gems.
in case you use bundler (Gemfile) you might also need to wrap the command invocation in:
Bundler.with_clean_env do
...
end
which will reset loaded bundler environment
Call:
result = `\path\to\ruby_1_8 \path\to\ruby_1_8_script.rb`
This will use the correct ruby binary to execute the script that expects it. The result is saved into the var.
You can call which ruby to find the version of the ruby in your current directory. Go to your project / source dir and call it to see the version (presumably Ruby 2) that you're using for the main app. Then, go to your old project / repo (associated with the 1.8 script) and run it again. Hopefully that will show you the path to Ruby 1.8. If not, try it from root (/). Or use RVM to confidently switch to Ruby 1.8 and then call it there to get the path.
I've never used RVM much. If it is confused, and filters things through the wrong gem set, etc, then you may need instead to switch to rbenv. Also, you may need to use its own functions to display the true path to the Ruby 1.8 binary (i.e. maybe it messes with which?) Again, I don't RVM.

ruby command line LoadError

When I try to include a library on the command line, I receive LoadError messages
$ ruby -v
ruby 1.8.7 (2012-06-29 patchlevel 370)
$ gem list | grep coderay_bash
coderay_bash (1.0.2)
$ ruby -rcoderay_bash /bin/coderay -v
ruby: no such file to load -- coderay_bash (LoadError)
$ ruby -rubygems -rcoderay_bash /bin/coderay -v
ruby: no such file to load -- coderay_bash (LoadError)
It looks to work with ruby 1.9.2
$ ruby -v
ruby 1.9.2p290 (2011-07-09)
$ ruby -rcoderay_bash /bin/coderay -v
CodeRay 1.0.7
In Ruby 1.8, anything you want to require that was installed with RubyGems cannot be accessed until you require 'rubygems'. 1.9 removes this requirement.
You have several options for this:
Just put require 'rubygems' at the top of your file. This is harmless for 1.9 and is probably the easiest thing, because it's in the code and no one using your app has to remember anything
Change your shebang line to #!/usr/bin/env ruby -rubygems This tells the Ruby interpreter to require rubygems, but allows users to avoid this by sending your file to ruby directly, if they are offended by RubyGems for some reason
Always run with ruby and use -rubygems, e.g. ruby -rubygems my_app.rb This has no dependencies on RubyGems in your code, and will work, but you have to remember to do it everytime, which is somewhat of a pain.

Rails 3 continues to use ruby 1.8.7 even though "ruby -v" states 1.9.2

I have finished developing my app and am currently deploying it. I'm getting a weird error from one of my controllers:
syntax error, unexpected ':', expecting ')'
When I run it in my dev environment it works fine. The only difference I can see between dev/prod is that dev is is using Ruby 1.9.2 and Prod is using 1.8.7. In my naivete I thought "Oh I'll just upgrade to 1.9.2" - what I fool I've been. I used RVM, changed my path, started new terminal session and now can see ruby is at 1.9.2:
ruby -v
ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-linux]
which ruby
/usr/local/rvm/rubies/ruby-1.9.2-p290/bin/ruby
However, when I run a "rails s" my computer continues to use 1.8.7:
[2011-10-05 05:58:40] INFO WEBrick 1.3.1
[2011-10-05 05:58:40] INFO ruby 1.8.7 (2010-08-16) [x86_64-linux]
[2011-10-05 05:58:45] INFO WEBrick::HTTPServer#start: pid=32574 port=3000
What the what!!!? I hope someone can help me. I'm about 15 minutes away from moving to the country, and become a farmer!
Thanks in advance.
if you used sudo when installed it might be the reason
sudo passenger-install-apache2-module
uses system ruby. Try
passenger-install-apache2-module
which should produce proper config files.
Have you tried rvm reload ? sometimes when I get strange errors like that reloading rvm fixes it.

Ruby cannot find required libraries even though gem is installed

I have spent literally days trying to install ruby 1.9.2 and get it working with gems :-/ I eventually gave up on my Mac OSX 10.6 machine and below is the current state on my Ubuntu machine. Any advice would be greatly appreciated!
# ruby test.rb
<internal:lib/rubygems/custom_require>:29:in `require': no such file to load -- mongo (LoadError)
from <internal:lib/rubygems/custom_require>:29:in `require'
from test.rb:1:in `<main>'
# cat test.rb
require 'mongo'
db = Mongo::Connection.new.db("mydb")
# gem which mongo
/usr/local/rvm/gems/ruby-1.9.2-p0/gems/mongo-1.1.2/lib/mongo.rb
# cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=10.10
DISTRIB_CODENAME=maverick
DISTRIB_DESCRIPTION="Ubuntu 10.10"
According to this page: http://docs.rubygems.org/read/chapter/19
I symlinked which ruby I was using to match that which gem is using:
# which ruby
/usr/local/rvm/bin/ruby
# ls -l `which ruby`
lrwxrwxrwx 1 root root 44 2010-11-17 13:25 /usr/local/rvm/bin/ruby -> /usr/local/rvm/rubies/ruby-1.9.2-p0/bin/ruby
# gem env | grep 'RUBY EXECUTABLE'
- RUBY EXECUTABLE: /usr/local/rvm/rubies/ruby-1.9.2-p0/bin/ruby
# which gem
/usr/local/rvm/bin/gem
# gem -v
1.3.7
# ruby -v
ruby 1.9.2p0 (2010-08-18 revision 29036) [i686-linux]
Try putting the following line at the beginning
require "rubygems"
Why is "rvm" displaying in your /usr/local/rvm/ path? Did you do a system-wide install, as a system administrator using administering Ruby system wide for multiple users?
Did you add [[ -s '/usr/local/lib/rvm' ]] && source '/usr/local/lib/rvm' to your ~/.bashrc, ~/.bash_profile or ~/.profile (whichever you have configured)?
For normal, every day use, I recommend RVM's default setup:
RVM installation, RVM gems management.
Note to self: Buy stock in RVM. It's too cool.
Does it work under Ruby 1.8.7, which is pre-installed by default on OS X?
If so, one difference between 1.9.1 and 1.9.2 is that "." isn't part of $:'s path any more.
I recommend that you do rvm implode and delete the current setup. Then use the railsready script to setup RVM and Ruby properly for you on Ubuntu. It's important to understand that until you know what you are doing you should run the script as a user. Hope that helps.
On linux and OS X, I have always had to put require "rubygems" in the beginning. However it has always worked fine without this line on windows.

Resources