Skip to content

Instantly share code, notes, and snippets.

@terotil
Created March 3, 2010 21:19
Show Gist options
  • Save terotil/321038 to your computer and use it in GitHub Desktop.
Save terotil/321038 to your computer and use it in GitHub Desktop.
# This is a snippet from my ~/.irbrc
# You need to gem install ruby2ruby ParseTree
# This is what you do with it
#
# > class Bar; define_method(:foo) { 'ou hai' }; end
# > puts Bar.to_ruby
# class Bar < Object
# def foo
# "ou hai"
# end
# end
require 'ruby2ruby'
require 'sexp_processor'
require 'unified_ruby'
require 'parse_tree'
# Monkey-patch to have Ruby2Ruby#translate with r2r >= 1.2.3, from
# http://seattlerb.rubyforge.org/svn/ruby2ruby/1.2.2/lib/ruby2ruby.rb
class Ruby2Ruby < SexpProcessor
def self.translate(klass_or_str, method = nil)
sexp = ParseTree.translate(klass_or_str, method)
unifier = Unifier.new
unifier.processors.each do |p|
p.unsupported.delete :cfunc # HACK
end
sexp = unifier.process(sexp)
self.new.process(sexp)
end
end
# The Beef
class Module
def to_ruby
Ruby2Ruby.translate(self)
end
end
@JoeyButler
Copy link

As of May 22, 2011 it seems that unified_ruby is merged into the parse_tree project. So you'll have to remove the "require 'unified_ruby'" line. Also to install parse_tree you have to run "gem install ParseTree" rather than using the underscore format.

Edit: Sorry I missed line 2 up top in the comments.

gem install ruby2ruby ParseTree

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment