开发者

Is Ruby2Ruby compatible with ParseTree?

require 'ruby2ruby'
require 'parsetree'

code = "puts(var)"
sexp = SexpProcessor.new.process(ParseTree.translate(code))
 # => s(:fcall, :puts, s(:array, s(:vcall, :var)))
code = Ruby2Ruby.new.process(sexp)
 # => UnknownNodeError: Bug! Unknown node-type :fcall to Ruby2Ruby

Is there some way to translate Sexps from ParseTree back to ruby code?

I started writing some code that would do this translation but I want to know if that already exists开发者_开发技巧. Another problem is the Ruby2Ruby puts a lot of unneeded parentheses in arithmetic operations (like 4+3-2+-2**4 to (((4 + 3) - 2) + -(2 ** 4)), both working equivalently). Is there some way to remove them?


I'm not sure if this works for you, because you seem to want to parse ruby code out of strings, but if you actually want the source of running code, you can do:

$ irb
?> require 'rubygems'
=> true 
?> require 'parse_tree'
=> true 
?> require 'parse_tree_extensions'
=> true 
?> require 'ruby2ruby'
=> true 
?> def calc; 4+3-2+-2**4; end
=> nil 
?> puts method(:calc).to_ruby
def calc
  (((4 + 3) - 2) + -(2 ** 4))
end

Although that does add the spacing that you didn't want.


I think they ought to be compatible, as they're written by the same person, but sometimes bugs creep in (as can be seen in this question which featured incompatibilities between two gems by the same author).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜