Nokogiri Segmentation fault?
You are forcing the Apple-installed Ruby to run, which is Ruby 1.8.7:
#!/usr/bin/ruby -w
instead of one of your Rubies managed by RVM. Try:
#!/usr/bin/env ruby -w
That way, if you want your system Ruby to run the code, you can tell RVM to switch to it:
rvm use system
and it will respond with: Now using system ruby.
Alternately, you can use any of the RVM managed Rubies to run the code:
rvm 1.8.7
if you had RVM install an instance of 1.8.7, or
rvm 1.9.2
or
rvm default
if you set up a default Ruby for RVM, which is always a good idea:
rvm use 1.9.2 --default
You can check to see what versions of Ruby RVM has under its control:
$ rvm list
rvm rubies
ruby-1.8.7-p334 [ x86_64 ]
=> ruby-1.9.2-p180 [ x86_64 ]
Now, moving to your actual code, you have a bug. When trying to retrieve the price for an item you're looking for the wrong CSS, not finding the price node, getting a nil
value, then trying to get the text
from it. Use this instead:
price = item.at_css(".camelPrice").text[/\$[0-9\.]+/]
Your output will look similar to:
Fisher-Price Power Wheels Batman Lil Quad Ride-On
- $59.97
/ip/Fisher-Price-Batman-Lil-Quad/10098697
After making the change to the #!
line, and the fix to the price
line, I ran your code using Ruby 1.8.7 in my system, along with RVM controlled 1.8.7 and 1.9.2 with no problems.
Rails Script Segmentation Fault with RVM
There's a problem with you RVM installation. which
should return
/Users/maletor/.rvm/rubies/ruby-1.9.2-p0/bin/ruby
Upgrade to the latest RVM installation. There was a bug in the 1.0 release with "shell path caching".
$ rvm get head
$ rvm reload
$ rvm repair all
$ rvm use 1.9.2
Segmentation fault error with ruby 1.9.3 when doing bundle install for Rails 3.2 app
I had a petty messed up ruby install at one point that was giving segmentation faults. I did
rvm implode
And started from scratch and in about an hour everything was working again
Why is Ruby throwing a Segmentation fault on only my system, and only in this Rails application?
Ok, I've fixed this on my machine. I'm not sure exactly why, but downgrading the ruby-debug gem to version 0.10.0 solves the problem for me.
The specific fix is running the following commands:
sudo gem uninstall ruby-debug
sudo gem install ruby-debug --version 0.10.0
Segmentation fault - Ruby 1.8.7 and Rails 3.1.3
A note from guides.rubyonrails.org says:
Note that Ruby 1.8.7 p248 and p249 have marshaling bugs that crash Rails 3.0. Ruby Enterprise Edition have these fixed since release 1.8.7-2010.02 though. On the 1.9 front, Ruby 1.9.1 is not usable because it outright segfaults on Rails 3.0, so if you want to use Rails 3 with 1.9.x jump on 1.9.2 for smooth sailing.
I've had very good luck with 1.9.2 and rvm makes it easy to switch between multiple versions of ruby. Would you be able to try this?
Nokogiri: node_set.rb:239: [BUG] Segmentation fault
I think it's bad practice to remove all the //item
nodes, then try to find them. Right there I can see trouble brewing.
This deletes all <item>
nodes from the document:
doc.xpath("//item").remove
This tries to find all <item>
nodes, which will return an empty NodeSet:
nodeset = doc.xpath("//item")
You don't show where api_doc
comes from, but if it's a Node that came from doc
, especially from before you removed the nodes, its state is suspicious because you might have some dangling references to removed <item>
nodes. As is, this tries to loop over all <item>
nodes, which might not exist, so an empty NodeSet could be returned, or worse, could be damaged:
api_doc.xpath("//item").each do |node|
node = check_score(node)
unless node.nil?
nodeset << node
end
end
I'd check the revisions for your Nokogiri and LibXML2 and make sure they're current. If not, update them. I'd also rethink the logic of removing all the <item>
nodes before you look for them.
Perhaps we could help you better if you explained what you're trying to do, and shared a small example of the XML.
Related Topics
Cannot Load Such File -- Rack/Handler/Puma
Is It a Good Idea to Purge Old Rails Migration Files
Ruby: How to Check If a String Contains Multiple Items
How to Cleanly Verify If the User Input Is an Integer in Ruby
Change Value of Request.Remote_Ip in Ruby on Rails
Rvm System-Wide Install Script Url Broken -- What Is Replacement
Trying to Compare Two Text Files, and Create a Third Based on Information
Problem with Rspec Test, Undefined Method 'Post'
Is There a Pry Debug Setup That Works with Ruby 2.0
Man in the Middle (Mitm) Proxy with Https Support
Use Windows or Linux to Start Work with Ruby on Rails
Semifixed: Missing 'Secret_Key_Base' for 'Production' Environment
Extract All Urls Inside a String in Ruby