Module: AWK

Included in:
IO, String, StringIO
Defined in:
lib/mawk.rb

Instance Method Summary collapse

Instance Method Details

#awk(script) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/mawk.rb', line 6

def awk(script)
  m = MAWK.new(script)

  self.each_line do |line|
    line.concat("\n") if line !~ /\n\Z/
    m << line
  end

  m.run_main
  m.uninitialize
end

#sawk(script) ⇒ Object



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
44
45
46
47
# File 'lib/mawk.rb', line 18

def sawk(script)
  file = Tempfile.new("mawk.#{$$}.#{Time.now.strftime '%Y%m%d%H%M%S'}")
  out = nil

  begin
    begin
      stdout = $stdout.clone
      $stdout.reopen(file)
      awk(script)
    ensure
      $stdout.reopen(stdout)
    end

    file.rewind

    if block_given?
      file.each_line do |line|
        yield(line.chomp)
      end
    else
      out = file.read
    end
  ensure
    $stdout.reopen(stdout)
    file.close
    file.unlink
  end

  return out
end