Rixiform Inc.

Scruffy Ruby Gem Debugging

July 16th 2008

In the process of creating some graphs in ruby I decided to go upstream from ruport and use the scruffy gem directly. I made some discoveries along the way that touch on issues that could be encountered by various permutations of scruffy, macport, and rubygems users.

A Storm Brewing

The first issue I encountered was this error:

testing_scruffy.rb:2:Warning: require_gem is obsolete.  Use gem instead.
/opt/local/lib/ruby/vendor_ruby/1.8/rubygems.rb:304:in `report_activate_error': Could not find RubyGem Scruffy (>= 0.0.0) (Gem::LoadError)
    from /opt/local/lib/ruby/vendor_ruby/1.8/rubygems.rb:238:in `activate'
    from /opt/local/lib/ruby/vendor_ruby/1.8/rubygems.rb:76:in `active_gem_with_options'
    from /opt/local/lib/ruby/vendor_ruby/1.8/rubygems.rb:61:in `require_gem'
    from testing_scruffy.rb:2

This seems like a straightforward case of not installing the gem. However, in my case the the gem was installed and there was additional strange behavior: require ‘scruffy’ would load the library in irb but not from a script (even with require ‘rubygems’ before it).This seemed inconsistent because $LOAD_PATH (aka $:) variables were identical in irb and in the script. After investigating a number of possibilities I updated rubygems (which was at 0.9.4) to 1.2.0 and this seem to resolve the blatantly bizarre behavior.

You can check which version of ruby gems you are running with “gem -v”. If you are using macports you may have an older version of ruby gems, in which case you may find it useful to update rb-rubygems. Caveat emptor: dependent updates may also be installed – such as ruby. Here’s how you can update your macport rb-rubygems library on OS X:

sudo port upgrade rb-rubygems

Scruffy Needs A Shave

If you do upgrade ruby gems you may run into the another issue. With an updated version of ruby gems the require_gem method is obsolete. Because of this you may see the following error since scruffy 0.2.2 is not updated to use the gem method instead of require_gem:

NoMethodError: undefined method `require_gem' for main:Object
    from /opt/local/lib/ruby/gems/1.8/gems/scruffy-0.2.2/lib/scruffy.rb:18
    from /opt/local/lib/ruby/vendor_ruby/1.8/rubygems/custom_require.rb:32:in `gem_original_require'
    from /opt/local/lib/ruby/vendor_ruby/1.8/rubygems/custom_require.rb:32:in `require'
    from (irb):3

I used the hack below to get around this and to avoid unpacking the gem in to my project directory for modifications. Once again caveat emptor – you may ultimately be better off unpacking (freezing) the gem into a local directory and including it in your application directly. Here’s the hack which simply creates an alias to suspend the deprecation of the require_gem method indefinitely:

require 'rubygems'
alias require_gem gem #later versions of gem use gem instead of require_gem
require 'scruffy'

Case Of The Missing Pie

Ah, but that’s not all. It appears that tutorials and documentation are out of sync with the rubygem download… see example code at http://scruffy.rubyforge.org/ which suggests this usage (with my require hack at the top):

require 'rubygems'
alias require_gem gem
require 'scruffy'

graph = Scruffy::Graph.new
graph.title = "Favourite Snacks"
graph.renderer = Scruffy::Renderers::Pie.new

graph.add :pie, '', {
      'Apple' => 20,
      'Banana' => 100,
      'Orange' => 70,
      'Taco' => 30

graph.render :to => "pie_test.svg"
graph.render :width => 300, :height => 200, :to => "pie_test.png", :as => 'png'

Running the sample code throws this error:

test_scruffy.rb:7: uninitialized constant Scruffy::Renderers::Pie (NameError)

Well, this error occurs because there is no such class as Scruffy::Renderers::Pie. It’s not in the docs and if you do grep -r “pie” . from within the scruffy-0.2.2 folder it comes up with nothing that would lead to the creation of such a class. FYI, the docs are visible online here http://scruffy.rubyforge.org/doc/

Well, it’s hard to use a class that doesn’t exist. It appears that this may be present in scruffy 0.2.3 released on July 13th – but as of this writing it’s not downloaded via sudo gem update scruffy. If you need this and it it is not yet available through ruby gems – you may need to do a manual install. In the mean time let’s try something else instead.

Steady Into Port (Mind The Wireshark)

graph = Scruffy::Graph.new
graph.title = "Sample Line Graph"
graph.renderer = Scruffy::Renderers::Standard.new

graph.add :line, 'Example', [20, 100, 70, 30, 106]

graph.render :to => "line_test.svg"
graph.render  :width => 300, :height => 200, :to => "line_test.png", :as => 'png'

Well if you have RMagick installed that may have worked for you. If not you will need to make sure rmagick is installed. On my machine I had to remove some dependencies before reinstalling tiff with different compilation options. Here are the commands I ran. You will probably have different dependencies, so know what you are doing. Don’t just blindly run these commands or you may uninstall something you need:

sudo port uninstall wireshark
sudo port uninstall gtk2
sudo port uninstall tiff @3.8.2_1 macosx
sudo port install tiff -macosx imagemagick  q8  gs  wmf
sudo gem install rmagick

Note that installing and compiling rmagick took several minutes. I would suggest looking at the rmagick installation page if you need to install this.

At this point you should be able to output your svg and png files.


Careful, or due to “environmental conditions” you could set out expecting a simple three hour tour happily outputting beautifully rendered svgs as you sail effortlessly through graph land; but instead, end up whistling along sympathetically to the Gilligan’s Island theme song as you set ground on the shore of an uncharted desert isle. Hopefully this will help you through some of the things you may run into, and you can have a smooth sail enjoying scruffy.