Class: String
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
-
.lorem(length = self.lorem_length) ⇒ String
This method chooses from lowercase letters a-z.
-
.lorem_length ⇒ Integer[ a random length suitable for a "lorem ipsum" string.
This method uses 1+rand(10).
-
.prev_char(chr) ⇒ String
(also: pred_char)
The previous character, with a changed flag and carry flag.
Instance Method Summary collapse
-
#capitalize_words ⇒ String
Self, with words capitalized.
-
#decrement(step = 1) ⇒ String
Decrement the rightmost natural number.
-
#increment(step = 1) ⇒ String
Increment the rightmost natural number.
-
#lowcase ⇒ String
Self in lowercase, with any non-word-characters replaced with single underscores (aka low dashes).
-
#prev ⇒ String
(also: #pred)
The previous string.
-
#prev! ⇒ String
(also: #pred!)
Do String#prev in place.
-
#split_tab ⇒ Array<String>
An array that is the string split at tabs, i.e.
-
#split_tsv ⇒ Array<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.
-
#to_class ⇒ Class
Ruby String#to_class method to convert from a String to a class.
-
#to_xid ⇒ String
The string as an XML id, which is the same as #lowcase.
-
#words ⇒ Array<String>
An array that is the string split into words, i.e.
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.
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_length ⇒ Integer[ a random length suitable for a "lorem ipsum" string.
This method uses 1+rand(10)
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.
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_words ⇒ String
Returns 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
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
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 |
#lowcase ⇒ String
Returns 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 |
#prev ⇒ String Also known as: pred
Returns the previous string.
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
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_tab ⇒ Array<String>
Returns 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_tsv ⇒ Array<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.
47 48 49 |
# File 'lib/sixarm_ruby_ramp/string.rb', line 47 def split_tsv split(/\n/).map{|line| line.split(/\t/)} end |
#to_class ⇒ Class
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/
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_xid ⇒ String
Returns 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 |