Class: Noiseless::AST::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/noiseless/ast.rb

Instance Method Summary collapse

Instance Method Details

#to_hObject Also known as: to_hash



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/noiseless/ast.rb', line 6

def to_h
  hash = {}
  instance_variables.each do |var|
    key = var.to_s.delete("@").to_sym
    value = instance_variable_get(var)

    hash[key] = case value
                when Node
                  value.to_h
                when Array
                  value.map { |item| item.is_a?(Node) ? item.to_h : item }
                else
                  value
                end
  end

  # Include the class name for better introspection
  hash[:_type] = self.class.name.split("::").last
  hash
end