Class: String

Inherits:
Object show all
Defined in:
lib/webget_ruby_ramp/string.rb

Overview

String extensions

Constant Summary collapse

ACCENTS =
Hash[*'à a á a â a ã a ä a å a ā a ă a æ ae ď d đ d ç c ć c č c ĉ c ċ c è e é e ê e ë e ē e ę e ě e ĕ e ė e ƒ f ĝ g ğ g ġ g ģ g ĥ h ħ h ì i ì i í i î i ï i ī i ĩ i ĭ i į j ı j ij j ĵ j ķ k ĸ k ł l ľ l ĺ l ļ l ŀ l
ñ n ń n ň n ņ n ʼn n ŋ n ò o ó o ô o õ o ö o ø o ō o ő o ŏ o ŏ o œ oek ą q ŕ r ř r ŗ r ś s š s ş s ŝ s ș s ť t ţ t ŧ t ț t ù u ú u û u ü u ū u ů u ű u ŭ u ũ u ų u ŵ w ý y ÿ y ŷ y ž z ż z ź z'.split]
LOWERCASE_ENGLISH_CHARS =

Helpful constants

['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
UPPERCASE_ENGLISH_CHARS =
['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.lorem(length = self.lorem_length) ⇒ Object

Return a random string suitable for “lorem ipsum” text.

This method chooses from lowercase letters a-z.

This method defaults to length = self.lorem_length.



194
195
196
# File 'lib/webget_ruby_ramp/string.rb', line 194

def self.lorem(length=self.lorem_length)
 ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'].choices(length).join
end

.lorem_lengthObject

Return a random length suitable for a “lorem ipsum” string.

This method uses 1+rand(10)



184
185
186
# File 'lib/webget_ruby_ramp/string.rb', line 184

def self.lorem_length
 1+rand(10)
end

.prev_char(chr) ⇒ Object Also known as: pred_char

Return the previous character, with a changed flag and carry flag

Examples

String.prev_char('n') => 'm', true, false   # change 
String.prev_char('a') => 'z', true, true    # change & carry
String.prev_char('6') => '5', true, false   # change
String.prev_char('0') => '9', true, true    # change & carry
String.prev_char('-') => '-', false, false  # unchanged


109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/webget_ruby_ramp/string.rb', line 109

def self.prev_char(chr) #=> prev_char, changed_flag, carry_flag
 case chr
 when '1'..'9', 'B'..'Z', 'b'..'z'
   pos=(chr.respond_to?(:ord) ? chr.ord : chr[0])
   return (pos-1).chr, true, false
 when '0'
   return '9', true, true
 when 'A'
   return 'Z', true, true
 when 'a'
   return 'z', true, true
 else
   return chr, false, false
 end
end

Instance Method Details

#capitalize_wordsObject

Return the string with words capitalized



13
14
15
# File 'lib/webget_ruby_ramp/string.rb', line 13

def capitalize_words
 split(/\b/).map{|word| word.capitalize }.join
end

#decrement(step = 1) ⇒ Object

Decrement the rightmost natural number

Example

'foo5bar'.decrement => 'foo4bar'
'foo5bar'.decrement(3) => 'foo2bar'
'foo10bar'.derement => 'foo9bar'
  • see String#increment



95
96
97
# File 'lib/webget_ruby_ramp/string.rb', line 95

def decrement(step=1)
 self=~/\d+/ ? $`+($&.to_i-step).to_s+$' : self
end

#increment(step = 1) ⇒ Object

Increment the rightmost natural number

Example

'foo5bar'.increment => 'foo4bar'
'foo5bar'.increment(3) => 'foo8bar'
'foo9bar'.increment => 'foo10bar'
  • see String#decrement



81
82
83
# File 'lib/webget_ruby_ramp/string.rb', line 81

def increment(step=1)
 self=~/\d+/ ? $`+($&.to_i+step).to_s+$' : self
end

#lowcaseObject

Return the string in lowercase, with any non-word-characters replaced with single underscores (aka low dashes).

Example

'Foo Goo Hoo' => 'foo_goo_hoo'
'Foo***Goo***Hoo' => 'foo_goo_hoo'


48
49
50
# File 'lib/webget_ruby_ramp/string.rb', line 48

def lowcase
 downcase.gsub(/[_\W]+/,'_')
end

#prevObject Also known as: pred

Return the previous string

c.f. String#next

Examples

'888'.prev => '887'
'n'.prev => 'm'
'N'.prev => 'M'

Examples with carry

'880'.prev => '879'
'nna'.prev => 'nmz'
'NNA'.prev => 'NMZ'
'nn0aA'.prev => 'nm9zZ'


140
141
142
# File 'lib/webget_ruby_ramp/string.rb', line 140

def prev
 self.clone.prev!
end

#prev!Object Also known as: pred!

Do String#prev in place



147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/webget_ruby_ramp/string.rb', line 147

def prev!
 return self if length==0
 index=length-1 # rightmost
 while true do
  chr=self[index].chr
  prev_chr,changed_flag,carry_flag=String.prev_char(chr)
   return self if !changed_flag
  self[index]=prev_chr
  return self if !carry_flag
  index-=1
  return nil if index<0
 end
end

#split_tabObject

Return an array that is the string split at tabs, i.e. split(/t/)



27
28
29
# File 'lib/webget_ruby_ramp/string.rb', line 27

def split_tab
 split(/\t/)
end

#split_tsvObject

Return an array that is the string split at newlines, then tabs. This is useful to split a TSV (Tab Separated Values) string into an array of rows, and each row into an array of fields.



36
37
38
# File 'lib/webget_ruby_ramp/string.rb', line 36

def split_tsv
  split(/\n/).map{|line| line.split(/\t/)}
end

#to_classObject

Ruby String#to_class method to convert from a String to a class

From Mirage at infovore.org/archives/2006/08/02/getting-a-class-object-in-ruby-from-a-string-containing-that-classes-name/



67
68
69
# File 'lib/webget_ruby_ramp/string.rb', line 67

def to_class
 split('::').inject(Kernel) {|scope, const_name| scope.const_get(const_name)}
end

#to_xidObject

Return the string as an XML id, which is the same as #lowcase

Example

"Foo Hoo Goo" => 'foo_goo_hoo'
"Foo***Goo***Hoo" => 'foo_goo_hoo'


59
60
61
# File 'lib/webget_ruby_ramp/string.rb', line 59

def to_xid
 self.lowcase
end

#wordsObject

Return an array that is the string split into words, i.e. split(W*b*)



20
21
22
# File 'lib/webget_ruby_ramp/string.rb', line 20

def words
 split(/\W*\b\W*/)
end