Module: Kernel

Defined in:
lib/blank.rb,
lib/string.rb,
lib/durations.rb,
lib/ruby/kernel/bench.rb,
lib/ruby/kernel/daemonize.rb,
lib/ruby/kernel/safe_fork.rb,
lib/ruby/kernel/non_verbose.rb

Instance Method Summary collapse

Instance Method Details

#bench(n = 1, &b) ⇒ Object



10
11
12
13
14
# File 'lib/ruby/kernel/bench.rb', line 10

def bench(n=1,&b)
	start = Time.now
	n.to_i.times(&b)
	Time.now - start
end

#Blank(*leave_methods) ⇒ Object

returns a blank class with only __* methods and those mentioned in leave_methods argument be aware that e.g. all Kernel methods are gone too, such as raise you can still use it via e.g. Kernel.raise or use leave_methods v0.1.1



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

def Blank(*leave_methods)
	@_blank_classes ||= {}
	leave_methods.push("__send__", "__id__", "initialize")
	leave_methods.flatten!
	leave_methods.uniq!
	leave_methods.sort!
	unless @_blank_classes[leave_methods] then
		slate = Class.new
		(slate.instance_methods+slate.private_instance_methods+slate.protected_instance_methods).each { |m|
			slate.send(:undef_method, m) unless leave_methods.include?(m)
		}
		@_blank_classes[leave_methods] = slate
	end
	@_blank_classes[leave_methods]
end

#daemonize(chdir = nil, &on_sighup) ⇒ Object

This method causes the current running process to become a daemon All further printing is relied to the error.log



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ruby/kernel/daemonize.rb', line 17

def daemonize(chdir=nil, &on_sighup)
	srand # Split rand streams between spawning and daemonized process
	safe_fork and exit # Fork and exit from the parent

	# Detach from the controlling terminal
	raise "Can't detach from controlling terminal" unless sess_id = Process.setsid

	# Prevent the possibility of acquiring a controlling terminal
	trap('SIGHUP', 'IGNORE')
	exit if safe_fork

	# In daemon mode, a SIGHUP means termination
	trap('SIGHUP', &on_sighup)

	# We can't get the originally controlling terminals stdout/stdin anymore
	STDIN.reopen("/dev/null")
	STDOUT.reopen("/dev/null", "a")
	STDERR.reopen(STDOUT)
	
	Dir.chdir(chdir) if chdir
	File.umask 0066

	sess_id
end

#non_verboseObject



10
11
12
13
14
15
16
# File 'lib/ruby/kernel/non_verbose.rb', line 10

def non_verbose
	old_verbose = $VERBOSE
	$VERBOSE    = false
	yield
ensure
	$VERBOSE = old_verbose
end

#safe_fork(delay = 5) ⇒ Object

Try to fork if at all possible retrying every delay sec (5s default) if the maximum process limit for the system has been reached



12
13
14
15
16
17
# File 'lib/ruby/kernel/safe_fork.rb', line 12

def safe_fork(delay=5)
	fork
rescue Errno::EWOULDBLOCK
	sleep(delay)
	retry
end