百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
U

unparser

> 编程语言
开源

将 Ruby AST 转换为语义上等价的 Ruby 源代码

330 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

将 Ruby AST 转换为语义上等价的 Ruby 源代码

unparser

Generate equivalent source for ASTs from parser.

The following constraints apply:

  • No support for macruby extensions
  • Only support for the modern AST format
  • Only support for Ruby >= 3.3

Notable Users:

  • mutant - Code review engine via mutation testing.
  • ruby-next - Ruby Syntax Backports.
  • Many other reverse-dependencies.

(if you want your tool to be mentioned here please PR the addition with a TLDR of your use case).

Public API:

While unparser is in the 0.x versions its public API can change any moment. I recommend to use ~> 0.x.y style version constraints that should give the best mileage.

Usage

ruby
require 'parser/current'
require 'unparser'

ast = Unparser.parse('your(ruby(code))')

Unparser.unparse(ast) # => 'your(ruby(code))'

To preserve the comments from the source:

ruby
require 'parser/current'
require 'unparser'

ast, comments = Unparser.parser.parse_with_comments(Unparser.buffer('your(ruby(code)) # with comments'))

Unparser.unparse(ast, comments: comments) # => 'your(ruby(code)) # with comments'

Passing in manually constructed AST:

ruby
require 'parser/current'
require 'unparser'

module YourHelper
  def s(type, *children)
    Parser::AST::Node.new(type, children)
  end
end

include YourHelper

node = s(:def,
  :foo,
  s(:args,
    s(:arg, :x)
  ),
  s(:send,
    s(:lvar, :x),
    :+,
    s(:int, 3)
  )
)

Unparser.unparse(node) # => "def foo(x)\n  x + 3\nend"

Note: DO NOT attempt to pass in nodes generated via AST::Sexp#s, these ones return API incompatible AST::Node instances, unparser needs Parser::AST::Node instances.

Equivalent vs identical:

…

ruby

-- encoding: binary --

"\x98\x76\xAB\xCD\x45\x32\xEF\x01\x01\x23\x45\x67\x89\xAB\xCD\xEF"


Original-AST:

(str "\x98v\xAB\xCDE2\xEF\x01\x01#Eg\x89\xAB\xCD\xEF")


Generated-Source:

```ruby
"\x98v\xAB\xCDE2\xEF\x01\x01#Eg\x89\xAB\xCD\xEF"

Generated-AST:

(str "\x98v\xAB\xCDE2\xEF\u0001\u0001#Eg\x89\xAB\xCD\xEF")

Diff:

@@ -1,2 +1,2 @@
-(str "\x98v\xAB\xCDE2\xEF\x01\x01#Eg\x89\xAB\xCD\xEF")
+(str "\x98v\xAB\xCDE2\xEF\u0001\u0001#Eg\x89\xAB\xCD\xEF")

Installation

Install the gem unparser via your prefered method.

People

Various people contributed to this repository. See Contributors.

Included Libraries

For dependency reduction reasons unparser ships vendored (and reduced) versions of:

  • abstract_type -> Unparser::AbstractType
  • adamantium -> Unparser::Adamantium
  • anima -> Unparser::Anima
  • equalizer -> Unparser::Equalizer
  • memoizable -> Unparser::Memoizable
  • mprelude -> Unparser::Either

Contributing

  • Fork the project.
  • Make your feature addition or bug fix.
  • Add tests for it. This is important so I don't break it in a future version unintentionally.
  • Commit, do not mess with version (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
  • Send me a pull request. Bonus points for topic branches.

Known Users

  • RailsRocket - A no-code app builder that creates Rails apps

License

See LICENSE file.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Rubyparserrubyruby-syntaxunparser

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言