Module: N::StringUtils

Defined in:
lib/glue/string.rb

Overview

General string utilities collection.

Design:

Implement as a module to avoid class polution. You can still Ruby’s advanced features to include the module in your class. Passing the object to act upon allows to check for nil, which isn’t possible if you use self.

TODO:

  • implement a method that returns easy to remember pseudo-random strings

  • add aliases for those methods in Kernel.

Constant Summary collapse

MATCH =

Apply a set of rules (regular expression matches) to the string

Requirements:

  • the rules must be applied in order! So we cannot use a hash because the ordering is not guaranteed! we use an

array instead.

Input:

the string to rewrite the array containing rule-pairs (match, rewrite)

Output:

the rewritten string

0
REWRITE =
1

Class Method Summary collapse

Class Method Details

.head(string, count = 128, force_cutoff = false, ellipsis = "...") ⇒ Object

returns short abstract of long strings (first ‘count’ characters, chopped at the nearest word, appended by ‘…’) force_cutoff: break forcibly at ‘count’ chars. Does not accept count < 2.



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/glue/string.rb', line 41

def self.head(string, count = 128, force_cutoff = false, ellipsis="...")
	return nil unless string
	return nil if count < 2

	if string.size > count
		cut_at = force_cutoff ? count : (string.index(' ', count-1) || count)
		xstring = string.slice(0, cut_at)
		return xstring.chomp(" ") + ellipsis
	else
		return string
	end
end

.random(max_length = 8, char_re = /[\w\d]/) ⇒ Object

Returns a random string. one possible use is password initialization.

Input:

the maximum length of the string

Output:

the random string



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/glue/string.rb', line 144

def self.random(max_length = 8, char_re = /[\w\d]/)
   # gmosx: this is a nice example of input parameter checking.
	# this is NOT a real time called method so we can add this
	# check. Congrats to the author.
	raise ArgumentError.new('char_re must be a regular expression!') unless char_re.is_a?(Regexp)
	
	string = ""

	while string.length < max_length
			ch = rand(255).chr
			string << ch if ch =~ char_re
	end

	return string
end

.rewrite(string, rules) ⇒ Object

Raises:

  • (ArgumentError)


72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/glue/string.rb', line 72

def self.rewrite(string, rules)
	return nil unless string

	# gmosx: helps to find bugs
	raise ArgumentError.new('The rules parameter is nil') unless rules

	rewritten_string = string.dup

	for rule in rules
		rewritten_string.gsub!(rule[MATCH], rule[REWRITE])
	end

	return (rewritten_string or string)
end

.valid?(string) ⇒ Boolean

Move this in String class?

Tests a string for a valid value (non nil, not empty)

Returns:

  • (Boolean)


32
33
34
# File 'lib/glue/string.rb', line 32

def self.valid?(string)
	return (not ((nil == string) or (string.empty?)))
end

.wrap(string, width = 20, separator = " ") ⇒ Object

Enforces a maximum width of a string inside an html container. If the string exceeds this maximum width the string gets wraped.

Not really useful, better use the CSS overflow: hidden functionality.

Input:

the string to be wrapped the enforced width the separator used for wrapping

Output:

the wrapped string

Example:

text = "1111111111111111111111111111111111111111111"
text = wrap(text, 10, " ")
p text # => "1111111111 1111111111 1111111111"

See the test cases to better understand the behaviour!



109
110
111
112
113
114
115
116
# File 'lib/glue/string.rb', line 109

def self.wrap(string, width = 20, separator = " ")
	return nil unless string

	re = /([^#{separator}]{1,#{width}})/
	wrapped_string = string.scan(re).join(separator)

	return wrapped_string
end