Module N::StringUtils
In: lib/glue/string.rb

StringUtils

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.

Methods

Constants

MATCH = 0   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

REWRITE = 1

Public Class methods

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.

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

Input:

the maximum length of the string

Output:

the random string

Replace dangerours chars in filenames

Screen an IP address

Convert the input string to greeklish.

Move this in String class?

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

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!

[Validate]