Guest User

Untitled

a guest
Oct 18th, 2013
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.98 KB | None | 0 0
  1. require 'pp'
  2.  
  3. def parse_class_info(instruction_sequence)
  4.   classes = []
  5.  
  6.   find_classes = proc do |instructions, parent, index|
  7.     next unless instructions.is_a? Array
  8.     if instructions[0] == :defineclass
  9.       _, class_name, class_info = instructions
  10.       info = { type: class_info[5][/\<([^:]+)/, 1], name: class_name }
  11.       class_body = class_info[13]
  12.       instruction, argument = parent[index - 3]
  13.       info[:super_class] = argument if instruction == :getconstant
  14.       classes << info
  15.       class_body.each {|instruction| find_classes.(instruction, class_body) }
  16.     else
  17.       instructions.each_with_index {|instruction, i| find_classes.(instruction, instructions, i) }
  18.     end
  19.   end
  20.  
  21.   find_classes.(instruction_sequence.to_a, nil)
  22.  
  23.   classes
  24. end
  25.  
  26. file_contents = <<-RUBY
  27. class TestModule < SuperClass
  28.   foo
  29. end
  30.  
  31. module TestMod
  32.   bar
  33. end
  34.  
  35. class TestClass2
  36.   foo_bar
  37. end
  38. RUBY
  39.  
  40. pp parse_class_info RubyVM::InstructionSequence.compile(file_contents)
Advertisement
Add Comment
Please, Sign In to add comment