sexp2ruby
sexp2ruby
generates ruby from ruby_parser S-expressions.
It is a fork of ruby2ruby with different goals and tools.
- Follows ruby-style-guide where possible
- Prefers OO design over performance
- Drops support for ruby 1.8.7
- Uses bundler instead of hoe
- Uses rspec instead of minitest
- Depends on (a small subset of) activesupport
When to Use ruby2ruby Instead
If you want to use the latest version of ruby_parser, please use
ruby2ruby instead. Ryan does not often make breaking changes to his
S-expression format, but when he does, ruby2ruby is more likely to keep up
to date. Following the ruby-style-guide is not a goal of ruby2ruby, so
you may want to use a tool like rubocop --auto-correct
. In fact, rubocop's
auto-correct is getting so good that it may not make sense to continue working
on this project.
Example
require 'ruby_parser'
require 'sexp2ruby'
ruby = "def a\n puts 'A'\nend\n\ndef b\n a\nend"
sexp = RubyParser.new.process(ruby)
# => s(:block, s(:defn, .. etc.
Sexp2Ruby::Processor.new.process(sexp.deep_clone)
# => "def a\n puts(\"A\")\nend\ndef b\n a\nend\n"
As with all SexpProcessor
s, Sexp2Ruby#process
destroys its input,
so deep_clone
as shown above if you need to preserve it.
Configuration
Configure output by passing options to Sexp2Ruby::Processor.new
:
hash = s(:hash, s(:lit, :a), s(:lit, 1))
Sexp2Ruby::Processor.new(hash_syntax: :ruby18).process
# => "{ :a => 1 }"
Sexp2Ruby::Processor.new(hash_syntax: :ruby19).process
# => "{ a: 1 }"
-
:hash_syntax
- either:ruby18
or:ruby19
. Default is:ruby19
. -
:no_paren_methods
- an array of symbols, these methods will omit argument parentheses. Default is[]
.