Class: Module

Inherits:
Object
  • Object
show all
Defined in:
lib/decompiler/module/as_code.rb

Instance Method Summary collapse

Instance Method Details

#as_code(indent = 0) ⇒ Object

TODO: it would be nice if we could go back and find the AST for the class instead of recreating the code from the class’s current state.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/decompiler/module/as_code.rb', line 8

def as_code(indent=0)
  imethods = self.instance_methods - self.superclass.instance_methods
  cmethods = self.instance_methods - self.superclass.instance_methods
  constants = self.constants - self.superclass.constants
  name = self.name.gsub(/.*::/, '')

  # TODO: included modules?
  if self.class == Class then
    s = "#{'  '*indent}class #{name} < #{self.superclass}\n"
  else
    s = "#{'  '*indent}module #{name}\n"
  end

  constants.each do |constant|
    s += "#{'  '*indent} #{constant}=#{self.const_get(constant).as_code}\n"
  end

  # TODO: protected/private
  imethods.each do |method|
    s += self.instance_method(method).as_code(indent+1)
    s += "\n"
  end

  cmethods.each do |method|
    s += self.instance_method(method).as_code(indent+1, "self.#{method}")
    s += "\n"
  end

  # TODO: singleton class constants
  # TODO: class variables
  # TODO: singleton instance variables

  s += "#{'  '*indent}end"

  return s
end