Class: Sourcerer::SourceCode

Inherits:
String
  • Object
show all
Defined in:
lib/sourcerer/source_code.rb

Direct Known Subclasses

ProcSource, ProcSourceBody

Instance Method Summary collapse

Instance Method Details

#convert_from_method!Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/sourcerer/source_code.rb', line 38

def convert_from_method!

  the_params= self.scan(/ *def *[\w\.]*[\( ] *(.*)/)[0][0]

  self.sub!(
      self.split("\n")[0],
      "Proc.new { |#{the_params}|"
  )

  return self

end

#convert_from_proc!Object



31
32
33
34
35
36
# File 'lib/sourcerer/source_code.rb', line 31

def convert_from_proc!

  self.sub!(/^[\w =]*Proc.new\s*{ */,'Proc.new { ')

  return self
end

#frequency(str) ⇒ Object

return the number how often the str is with in the self by default with b regex border



6
7
8
9
10
11
12
13
# File 'lib/sourcerer/source_code.rb', line 6

def frequency(str)
  begin
    if str.class == String
      str= '\b'+str+'\b'
    end
  end
  self.scan(/#{str}/).count
end

#source_formater_for_line_sub(trim_comment_out = true) ⇒ Object

this is a helper to create source strings from procs



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/sourcerer/source_code.rb', line 16

def source_formater_for_line_sub trim_comment_out= true

  if trim_comment_out == true
    trim_comment= self.match(/^.*#/)
    unless trim_comment.nil?
      self.replace trim_comment[0][0..(trim_comment[0].length-2)].concat("\n")
    end
  end

  self.frequency(/{/)+
      self.frequency(/def/)-
      self.frequency(/}/)

end