module LX

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

Constants

VERSION

version

Public Class Methods

block() { || ... } click to toggle source

Same as LX::scope.

# File lib/lx.rb, line 78
def self.block
        yield
end
deep_dup(obj, opts={}) click to toggle source

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

# 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) click to toggle source

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.

# 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
scope() { || ... } click to toggle source

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

# File lib/lx.rb, line 73
def self.scope
        yield
end

Private Class Methods

deep_dup_array(arr, opts) click to toggle source
# File lib/lx.rb, line 154
def self.deep_dup_array(arr, opts)
        rv = []
        
        arr.each do |val|
                rv.push self.deep_dup(val, opts)
        end
        
        return rv
end
deep_dup_hash(hsh, opts) click to toggle source
# File lib/lx.rb, line 134
def self.deep_dup_hash(hsh, opts)
        # $tm.hrm
        rv = {}
        
        hsh.each do |key, val|
                rv[key.dup] = self.deep_dup(val, opts)
        end
        
        return rv
end