Module: Kernel

Defined in:
lib/poolparty/core/kernel.rb

Overview

Kernel overloads

Instance Method Summary collapse

Instance Method Details

#as(klass_or_obj, &block) ⇒ Object



27
28
29
# File 'lib/poolparty/core/kernel.rb', line 27

def as(klass_or_obj, &block)
  block.in_context(klass_or_obj).call
end

#callstack(level = 1) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/poolparty/core/kernel.rb', line 11

def callstack( level = 1 )
  call_str_array = caller(level)
  stack = []
  call_str_array.each{ |call_str|
    file, lineno, method = call_str.split(':')
    if method =~ /in `(.*)'/ then
      method = $1.intern()
    end
    stack << [file, lineno.to_i, method]
  }
  stack
end

#capture_stdout(&block) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/poolparty/core/kernel.rb', line 38

def capture_stdout(&block)
   old_stdout = $stdout
   out = StringIO.new
   $stdout = out
   begin
      block.call if block
   ensure
      $stdout = old_stdout
   end
   out.string
end

#get_latest_callerObject



6
7
8
9
10
# File 'lib/poolparty/core/kernel.rb', line 6

def get_latest_caller
  returning Array.new do |arr|
    callstack.size.times {|i| arr << callstack[i][0] unless callstack[i][0] =~ /lib\/poolparty/ }
  end.first
end

#hide_outputObject

redirect stdout and stderr to /dev/null and reopen after block



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/poolparty/core/kernel.rb', line 51

def hide_output
  begin
    old_stdout = STDOUT.dup
    old_stderr = STDERR.dup
    STDOUT.reopen(File.open((PLATFORM =~ /mswin/ ? "NUL" : "/dev/null"), 'w'))
    STDERR.reopen(File.open((PLATFORM =~ /mswin/ ? "NUL" : "/dev/null"), 'w'))
    yield if block_given?
  ensure
    STDOUT.flush
    STDOUT.reopen(old_stdout)
    STDERR.flush
    STDERR.reopen(old_stderr)
  end
end

#wait(time = 5) ⇒ Object

Nice wait instead of sleep



24
25
26
# File 'lib/poolparty/core/kernel.rb', line 24

def wait(time=5)
  sleep time.is_a?(String) ? eval(time) : time
end

#with_warnings_suppressedObject



30
31
32
33
34
35
36
# File 'lib/poolparty/core/kernel.rb', line 30

def with_warnings_suppressed
  saved_verbosity = $-v
  $-v = nil
  yield
ensure
  $-v = saved_verbosity
end