Posted by Seamus on Friday, December 17, 2010.

How we replaced ParseTree in Ruby 1.9

Like many Ruby shops, we took for granted ParseTree’s ability to show the source code of pretty much any object. As previously explained, however, ParseTree doesn’t work with Ruby 1.9.2. We fixed the problem by using sourcify.

## DOESN'T WORK IN RUBY 1.9
gem 'ParseTree', :require => false
require 'parse_tree'
require 'parse_tree_extensions'

## WORKS IN RUBY 1.9
gem 'sourcify'
gem 'ruby_parser'
gem 'file-tail'

In particular, we replaced ParseTree’s Proc#to_ruby with sourcify’s Proc#to_source.

## The old ParseTree way
proc.to_ruby
## The sourcify way - but raised NoMatchingProcError or MultipleMatchingProcsPerLineError
proc.to_source
## The sourcify way - giving :attached_to a symbol to help it find the correct Proc
proc.to_source :attached_to => :quorum

We needed to pass the :attached_to option because our carbon calculation code has multiply nested procs and we would get NoMatchingProcError or MultipleMatchingProcsPerLineError:

committee :distance do
  quorum 'from airports' do
    # [...]
  end
  quorum 'from cohort' do
    # [...]
  end
  quorum 'default' do
    # [...]
  end
end

Thanks to sourcify’s author, Ng Tze Yang, who added the :attached_to option when we showed him the problem. It made it possible to migrate our emission estimate web service, which comes with detailed carbon calculation methodology reports, to Ruby 1.9!

What blog is this?

Safety in Numbers is Brighter Planet's blog about climate science, Ruby, Rails, data, transparency, and, well, us.

Who's behind this?

We're Brighter Planet, the world's leading computational sustainability platform.

Who's blogging here?

  1. Patti Prairie CEO