Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想将 Psych 生成的 AST 转储到 JSON。我怎样才能做到这一点?
gem 'psych' require 'psych' require 'json' ast = Psych.parse_stream("me: 1"); p ast
ast.to_json没有给我 JSON 树。
ast.to_json
您不想直接将 AST 转换为 JSON。您首先将其转换为 Ruby 对象图to_ruby。如果像这样更改,您的代码可以工作:
to_ruby
gem 'psych' require 'psych' require 'json' ast = Psych.parse_stream("me: 1"); p ast.to_ruby[0].to_json