class Sexp2Ruby::Node::Hash

Constants

HASH_VAL_NO_PAREN

Some sexp types are OK without parens when appearing as hash values. This list can include `:call`s because they're always printed with parens around their arguments. For example:

{ :foo => (bar("baz")) } # The outer parens are unnecessary
{ :foo => bar("baz") }   # This is the normal code style

Public Instance Methods

to_s(exp) click to toggle source
# File lib/sexp2ruby/node/hash.rb, line 23
def to_s(exp)
  result = []

  until exp.empty?
    s = exp.shift
    t = s.sexp_type
    ruby19_key = ruby19_hash_key?(s)
    lhs = process s

    case t
    when :kwsplat then
      result << lhs
    else
      rhs = exp.shift
      t = rhs.first
      rhs = process rhs
      rhs = "(#{rhs})" unless HASH_VAL_NO_PAREN.include? t

      if hash_syntax == :ruby19 && ruby19_key
        lhs.gsub!(/\A:/, "")
        result << "#{lhs}: #{rhs}"
      else
        result << "#{lhs} => #{rhs}"
      end
    end
  end

  result.empty? ? "{}" : "{ #{result.join(', ')} }"
end