Skip to content

Instantly share code, notes, and snippets.

@headius
Created February 23, 2012 17:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save headius/1893931 to your computer and use it in GitHub Desktop.
Save headius/1893931 to your computer and use it in GitHub Desktop.
require 'rjb'
Rjb::load(classpath = 'jrubyparser-0.2.jar', jvmargs=[])
module ToSource
SR = Rjb::import('java.io.StringReader')
PC = Rjb::import('org.jrubyparser.parser.ParserConfiguration')
P = Rjb::import('org.jrubyparser.Parser')
RWV = Rjb::import('org.jrubyparser.rewriter.ReWriteVisitor')
SW = Rjb::import('java.io.StringWriter')
end
class String
def to_ast
ToSource::P.new.parse('(string)', ToSource::SR.new(self), ToSource::PC.new)
end
end
class Rjb::Rjb_JavaProxy
def to_s
toString
end
end
''.to_ast.class.class_eval do
def to_source
sw = ToSource::SW.new
rwv = ToSource::RWV.new(sw, '(string)')
accept(rwv)
sw.toString
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment