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.



215
216
217
# File 'lib/webget_ruby_ramp/string.rb', line 215

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)



205
206
207
# File 'lib/webget_ruby_ramp/string.rb', line 205

def self.lorem_length
 1+rand(10)
end

.prev_char(c) ⇒ 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


130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/webget_ruby_ramp/string.rb', line 130

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

Instance Method Details

#capitalize_wordsObject

Return the string with words capitalized



34
35
36
# File 'lib/webget_ruby_ramp/string.rb', line 34

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



116
117
118
# File 'lib/webget_ruby_ramp/string.rb', line 116

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



102
103
104
# File 'lib/webget_ruby_ramp/string.rb', line 102

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'


69
70
71
# File 'lib/webget_ruby_ramp/string.rb', line 69

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'


161
162
163
# File 'lib/webget_ruby_ramp/string.rb', line 161

def prev
 self.clone.prev!
end

#prev!Object Also known as: pred!

Do String#prev in place



168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/webget_ruby_ramp/string.rb', line 168

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/)



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

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.



57
58
59
# File 'lib/webget_ruby_ramp/string.rb', line 57

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/



88
89
90
# File 'lib/webget_ruby_ramp/string.rb', line 88

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'


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

def to_xid
 self.lowcase
end

#wordsObject

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



41
42
43
# File 'lib/webget_ruby_ramp/string.rb', line 41

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