public
Last active

  • Download Gist
module_to_ruby.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
# 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

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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.