Archive for the ‘Ruby’ Category

How to get OpenSSL in Ruby 1.9.3 working on OSX 10.7 (fixing the Segmentation Fault with Ruby OpenSSL)

Published by Rob on January 20th, 2012 - in Ruby

update – this needs to have a recent version of RVM – it works with rvm 1.10 but not with 1.6 – do an rvm update first.

When using the mighty cobweb web crawler on my OSX 10.7 with Ruby 1.9.3 I was getting a seg fault in net http:

net/http.rb:799: [BUG] Segmentation fault

A bit of googling and isolation in IRB showed that this can be caused when hitting HTTPS urls using net http.

I saw a few different solutions to the problem, primarily talking about Ruby 1.9.2.Surprisingly it took me a while to end up at the RVM documentation about the problem: https://rvm.beginrescueend.com/packages/openssl/. This didn’t quite work out of the box for me, so I used the solution in the following gist.

rvm pkg install openssl
rvm remove 1.9.3
rvm install 1.9.3 --with-openssl-dir=$rvm_path/usr --with-gcc=clang
view raw file1.sh This Gist is brought to you using Simple Gist Embed.

and now I can happily crawl https urls.

Two tricks for getting Backbone.js to play well with Ruby on Rails

Published by Rob on November 29th, 2011 - in Ruby

Backbone.js is a great library for creating single page applications that consume RESTful JSON services. Ruby on Rails does well at generating RESTful APIs and speaking JSON. In order to get the Rails working really well with Backbone, there are two tricks to do.

First, add the following line to the application controller to make the output look like what backbone expects.

...
ActiveRecord::Base.include_root_in_json = false
...

Second take advantage of the : only option in to_json to limit the attributes included in the serialised json object. The documentation for this isn’t quite there in the latest rails, but the 2.3 to_json method describes it. http://apidock.com/rails/ActiveRecord/Serialization/to_json.

# code to retrieve the collection - the to_json filtering works on a collection, or a single model record
render :json => collection.to_json(:only => [ :id, :name, :another_attribute])

With these two simple tricks rails will generate the json that backbone wants, and you’ll have a happy front-end developer.

Updating RubyGems in OSX 10.5.7

Published by Rob on May 27th, 2009 - in Development, Ruby

.7When recently trying to install Sinatra via RubyGems, I got a message that RubyGems was out of date. I figured that gem would be smart enough to have an easy upgrade command, so there had to be a command to easily upgrade. Naturally there is:

1
<span style=" background-color: #ffffcc;">gem update --system</span>

I only found this when looking through google, and I got a series of pages warning to be careful when using

1
<span style=" background-color: #ffffcc;">gem update --system</span>

as it can kill existing gems (http://puctuatedproductivity.com/2007/11/01/unistalling-ruby-installed-by-source-on-os-x, http://thenoobonrails.blogspot.com/2008/06/doing-gem-update-system-might-lose-all.html) so I was a bit nervous.  Since I have a periodic use of ruby and I'm lazy enough to make Larry Wall proud, I figured I'd take a punt on just using

1
<span style=" background-color: #ffffcc;">gem update --system</span>

.  Turns out it just works, and I've kept all my old gems.  Hooray.  Given that the posts talking about issues are old, I'm either assuming that they've done things differently to me, or things have been fixed since then… so… if you need to update gems due to a message:

ERROR: Error installing sinatra:
fastthread requires RubyGems version >= 1.2

or similar, just use

1
<span style=" background-color: #ffffcc;">gem update --system</span>

© Rob@Rojotek