Module: LX

Defined in:
lib/lx.rb

Overview

The LX module is the home for utilities that don’t particularly fit in one class.

Defined Under Namespace

Modules: File Classes: Array, Hash, Integer, NilClass, String

Constant Summary collapse

VERSION =

version

'1.2'
@@RAND_CHARS =

characters for random string

%w(
	b c d f h m p q r s t v w x z
	B C D F H M P Q R S T V W X Z
	2 3 4 5 7 8 9
)
@@RND_CHARS_MAX =

length of random character string

@@RAND_CHARS.length - 1
@@RND_PK_LENGTH =

target length

20

Class Method Summary collapse

Class Method Details

.blockObject

Same as LX::scope.



78
79
80
# File 'lib/lx.rb', line 78

def self.block
	yield
end

.deep_dup(obj, opts = {}) ⇒ Object

Deep suplicates a hash or array. Don’t call this method directly. Call Array#lx#deep_dup or Hash#lx#deep_dup.



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/lx.rb', line 94

def self.deep_dup(obj, opts={})
	# $tm.hrm
	rv = nil
	
	# default options
	opts = {'done'=>{}}.merge(opts)
	
	# error if object has already been cloned
	if obj.is_a?(::Array) or obj.is_a?(::Hash)
		if opts['done'][obj.object_id]
			raise "object #{obj.object_id} has already been dupped"
		end
	end
	
	if obj.is_a?(::Array)
		rv = deep_dup_array(obj, opts)
	elsif obj.is_a?(::Hash)
		rv = deep_dup_hash(obj, opts)
	else
		begin
			rv = obj.dup
		rescue
			rv = obj
		end
	end
	
	# note object as done
	opts['done'][obj.object_id] = true
	
	# return rv
	return rv
end

.randstr(len = @@RND_PK_LENGTH) ⇒ Object

Returns a random string of characters. The optional single param is the length of the string to return. By default, the string is 20 characters long.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/lx.rb', line 47

def self.randstr(len=@@RND_PK_LENGTH)
	# intialize rv
	rv = ''
	
	# build return string
	len.times do
		rv += @@RAND_CHARS[rand 0 .. @@RND_CHARS_MAX]
	end
	
	# freeze return value
	rv.freeze
	
	# return
	return rv
end

.scopeObject

A little method that does nothing more than allow you to create a ‘do’ block.



73
74
75
# File 'lib/lx.rb', line 73

def self.scope
	yield
end