Class: String

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

Overview

String extensions

Constant Summary collapse

LOWERCASE_ENGLISH_CHARS =

Helpful constants

('a'..'z').to_a
UPPERCASE_ENGLISH_CHARS =
('A'..'Z').to_a

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.lorem(length = self.lorem_length) ⇒ String

This method chooses from lowercase letters a-z.

This method defaults to length = self.lorem_length.

Examples:

String.lorem => "galkjadscals"
String.lorem(4) => "qtgf"

Returns:

  • (String)

    a random string suitable for “lorem ipsum” text.



223
224
225
# File 'lib/sixarm_ruby_ramp/string.rb', line 223

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'].sample(length).join
end

.lorem_lengthInteger[ a random length suitable for a "lorem ipsum" string.

This method uses 1+rand(10)

Examples:

String.lorem_length => 3
String.lorem_length => 9
String.lorem_length => 5

Returns:

  • (Integer[ a random length suitable for a "lorem ipsum" string.)

    Integer[ a random length suitable for a “lorem ipsum” string.



209
210
211
# File 'lib/sixarm_ruby_ramp/string.rb', line 209

def self.lorem_length
 1+rand(10)
end

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

Returns 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

Returns:

  • (String)

    the previous character, with a changed flag and carry flag



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/sixarm_ruby_ramp/string.rb', line 127

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_wordsString

Returns self, with words capitalized.

Examples:

"foo goo hoo".capitalize_words
 => "Foo Goo Hoo"

Returns:

  • (String)

    self, with words capitalized



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

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

#decrement(step = 1) ⇒ String

Decrement the rightmost natural number

Examples:

'foo5bar'.decrement => 'foo4bar'
'foo5bar'.decrement(3) => 'foo2bar'
'foo10bar'.derement => 'foo9bar'

Returns:

  • (String)

    the string with a decremented rightmost number

See Also:



113
114
115
# File 'lib/sixarm_ruby_ramp/string.rb', line 113

def decrement(step=1)
 increment(-step)
end

#increment(step = 1) ⇒ String

Increment the rightmost natural number

Examples:

'foo5bar'.increment => 'foo4bar'
'foo5bar'.increment(3) => 'foo8bar'
'foo9bar'.increment => 'foo10bar'

Returns:

  • (String)

    the string with an incremented rightmost number

See Also:



97
98
99
# File 'lib/sixarm_ruby_ramp/string.rb', line 97

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

#lowcaseString

Returns self in lowercase, with any non-word-characters replaced with single underscores (aka low dashes).

Examples:

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

Returns:

  • (String)

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



60
61
62
# File 'lib/sixarm_ruby_ramp/string.rb', line 60

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

#prevString Also known as: pred

Returns the previous string.

Examples:

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

with carry

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

Returns:

  • (String)

    the previous string

See Also:

  • #next


158
159
160
# File 'lib/sixarm_ruby_ramp/string.rb', line 158

def prev
 self.clone.prev!
end

#prev!String Also known as: pred!

Do String#prev in place

Returns:



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

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
 return self
end

#split_tabArray<String>

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

Examples:

"foo\tgoo\thoo".split_tab
=> ["foo", "goo", "hoo"]

Returns:

  • (Array<String>)

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



33
34
35
# File 'lib/sixarm_ruby_ramp/string.rb', line 33

def split_tab
 split(/\t/)
end

#split_tsvArray<String>

This is useful to split a TSV (Tab Separated Values) string into an array of rows, and each row into an array of fields.

Examples:

"foo\tgoo\thoo\n"ioo\tjoo\tkoo\nloo\tmoo\tnoo".split_tsv
=> [["foo", "goo", "hoo"], ["ioo", "joo", "koo"], ["loo", "moo", "noo"]]

Returns:

  • (Array<String>)

    an array that is the string split at newlines, then tabs.



47
48
49
# File 'lib/sixarm_ruby_ramp/string.rb', line 47

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

#to_classClass

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/

Returns:

  • (Class)

    the string converted to a class



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

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

#to_xidString

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

Examples:

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

Returns:

  • (String)

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



71
72
73
# File 'lib/sixarm_ruby_ramp/string.rb', line 71

def to_xid
 self.lowcase
end

#wordsArray<String>

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

Examples:

"foo goo hoo".words
=> ["foo", "goo", "hoo"]

Returns:

  • (Array<String>)

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



23
24
25
# File 'lib/sixarm_ruby_ramp/string.rb', line 23

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