Class: Sexp2Ruby::Node::Hash

Inherits:
Base
  • Object
show all
Defined in:
lib/sexp2ruby/node/hash.rb

Constant Summary collapse

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
[
  :call,
  :false,
  :hash,
  :lit,
  :lvar,
  :nil,
  :str,
  :true
]

Constants inherited from Base

Base::ASSIGN_NODES, Base::LF, Base::LINE_LENGTH

Instance Attribute Summary

Attributes inherited from Base

#processor

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Sexp2Ruby::Node::Base

Instance Method Details

#to_s(exp) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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