Module: Useful::RubyExtensions::Object
- Included in:
- Object
- Defined in:
- lib/useful/ruby_extensions/object.rb
Defined Under Namespace
Modules: FromActivesupport
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.included(receiver) ⇒ Object
105
106
107
|
# File 'lib/useful/ruby_extensions/object.rb', line 105
def self.included(receiver)
receiver.send :include, FromActivesupport
end
|
Instance Method Details
#capture_std_output ⇒ Object
19
20
21
22
23
24
25
26
27
28
29
|
# File 'lib/useful/ruby_extensions/object.rb', line 19
def capture_std_output
out = ::StringIO.new
err = ::StringIO.new
$stdout = out
$stderr = err
yield
return out, err
ensure
$stdout = STDOUT
$stderr = STDERR
end
|
#false? ⇒ Boolean
Also known as:
is_false?
9
10
11
|
# File 'lib/useful/ruby_extensions/object.rb', line 9
def false?
self == false
end
|
#sudo(cmd, logger = nil, &block) ⇒ Object
Invokes a system command using “ and pass the status and result to a given block
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/useful/ruby_extensions/object.rb', line 33
def sudo(cmd, logger=nil, &block)
begin
require 'open4' unless defined?(::Open4)
rescue LoadError => err
puts "you need open4 to run the sudo helper: gem install open4"
else
logger.debug "`sudo #{cmd}` ..." if logger && logger.respond_to?(:debug)
result = {}
status = Open4.popen4("sudo #{cmd}") do |pid, stdin_io, stdout_io, stderr_io|
result[:out] = stdout_io.gets
result[:err] = stderr_io.gets
end
if block
block.call(status, status.success? ? result[:out] : result[:err])
else
status
end
end
end
|
#true? ⇒ Boolean
Also known as:
is_true?
14
15
16
|
# File 'lib/useful/ruby_extensions/object.rb', line 14
def true?
self == true
end
|