sexp2ruby

Build Status Code Climate

sexp2ruby generates ruby from RubyParser S-expressions. It is a fork of ruby2ruby with slightly different goals.

  • Follows ruby-style-guide where possible
  • Prefers OO design over performance
  • Drops support for ruby 1.8.7
  • Depends on (a small subset of) activesupport
  • Uses bundler instead of hoe
  • Uses rspec instead of minitest

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 SexpProcessors, 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 [].