Class: String
Class Method Summary collapse
-
.random(length = 8, seed = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789') ⇒ Object
Generate a random string with a given length and seed.
Instance Method Summary collapse
-
#camelcase(cap_first = false) ⇒ Object
Camelize string.
-
#camelcase!(cap_first = false) ⇒ Object
Destructive version of String#camelcase.
-
#capitalize_words(*disclude) ⇒ Object
Capitalize words.
-
#chars ⇒ Object
Split string into an array of characters.
-
#join(*arr) ⇒ Object
The ugly and well…
-
#munge ⇒ Object
My unsubmitted answer to a previous RubyQuiz question.
-
#munge! ⇒ Object
Destructive version of String#munge.
-
#nothing? ⇒ Boolean
Checks if a string is nothing but whitespace or is empty.
-
#ord ⇒ Object
For anyone who still misses PHP or Python’s ord() functions…
-
#shuffle ⇒ Object
Randomly shuffle a string.
-
#shuffle! ⇒ Object
Destructive version of String#shuffle.
-
#to_proc ⇒ Object
Convenience method for things such as array mapping.
-
#to_rxp ⇒ Object
Convert a string to a Regexp object.
-
#truncate(length, ending = '') ⇒ Object
Truncate a string to a certain length, and optionally append ending characters to it.
-
#truncate!(length, ending = '') ⇒ Object
Destructive version of String#truncate.
-
#underscore ⇒ Object
Removes everything except letters and spaces, replaces spaces with an underscore, and lowercases the string.
-
#underscore! ⇒ Object
Destructive version of String#underscore.
-
#words ⇒ Object
Get an array of “words”.
-
#wrap(width = 80, separator = $/) ⇒ Object
Wrap string by characters and join them by a specified separator.
-
#wrap!(width = 80, separator = $/) ⇒ Object
Destructive version of String#wrap.
Class Method Details
.random(length = 8, seed = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789') ⇒ Object
Generate a random string with a given length and seed.
Example: String.random(4, 'abcdefg') #=> "cdeg"
Returns: String
8 9 10 |
# File 'lib/extra/string.rb', line 8 def self.random(length = 8, seed = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789') s = ''; length.times { s << seed.shuffle[0] }; s end |
Instance Method Details
#camelcase(cap_first = false) ⇒ Object
Camelize string.
Example: 'what is my name'.camelcase #=> "whatIsMyName"
Returns: String
141 142 143 144 145 146 147 148 |
# File 'lib/extra/string.rb', line 141 def camelcase(cap_first = false) if size > 1 camelcased = capitalize_words.gsub(/[^a-z]/i, '') camelcased[0,1] = camelcased[0,1].downcase unless cap_first else self end end |
#camelcase!(cap_first = false) ⇒ Object
Destructive version of String#camelcase.
Returns: String or nil.
154 155 156 157 |
# File 'lib/extra/string.rb', line 154 def camelcase!(cap_first = false) camelcased = camelcase(cap_first) self[0..-1] = camelcased unless camelcased == self end |
#capitalize_words(*disclude) ⇒ Object
Capitalize words. Example: 'The dog is stupid'.capitalize_words('is') #=> "The Dog is Stupid"
Returns: String with words capitalized.
80 81 82 83 |
# File 'lib/extra/string.rb', line 80 def capitalize_words(*disclude) disclude = disclude.flatten gsub(/\w+/u) { |word| disclude.include?(word) ? word : word.capitalize } end |
#chars ⇒ Object
Split string into an array of characters. Should be multi-byte safe…
Example: 'foo'.chars #=> ["f", "o", "o"]
Returns: Array
71 72 73 |
# File 'lib/extra/string.rb', line 71 def chars split(//u) end |
#join(*arr) ⇒ Object
The ugly and well… unneeded Pythonic join method. Thanks to manveru for pointing out the interesting way of using array expansion with a combination of Array#flatten to support multiple arguments.
Example: ', '.join([1, 2, 3]) #=> "1, 2, 3"
Returns: The joined string.
39 40 41 |
# File 'lib/extra/string.rb', line 39 def join(*arr) arr.flatten.join(self) end |
#munge ⇒ Object
My unsubmitted answer to a previous RubyQuiz question. Basically #munge will take words, scramble only the middle contents of the word while the first and last letters remain intact.
Example: 'You look like a terrifying goblin'.munge #=> "You look lkie a tiifyenrrg goilbn"
Returns: Munged string
93 94 95 96 97 98 99 100 101 |
# File 'lib/extra/string.rb', line 93 def munge gsub(/\w+/u) do |word| if word.size > 2 word[0,1] + word[1...-1].shuffle + word[-1,1] else word end end end |
#munge! ⇒ Object
Destructive version of String#munge.
Returns: Munged string or nil.
107 108 109 110 |
# File 'lib/extra/string.rb', line 107 def munge! munged = munge self[0..-1] = munged unless munged == self end |
#nothing? ⇒ Boolean
Checks if a string is nothing but whitespace or is empty.
Example: " ".nothing? #=> true
Returns: True or false
224 225 226 |
# File 'lib/extra/string.rb', line 224 def nothing? strip.empty? end |
#ord ⇒ Object
For anyone who still misses PHP or Python’s ord() functions… which I’ve learned to not need in Ruby, here it is.
Example: 'a'.ord #=> 97
Returns: Fixnum or nil
61 62 63 |
# File 'lib/extra/string.rb', line 61 def ord self[0] end |
#shuffle ⇒ Object
Randomly shuffle a string.
Example: 'foobar'.shuffle #=> bofoar
Returns: String
18 19 20 |
# File 'lib/extra/string.rb', line 18 def shuffle split(//u).sort_by { rand }.join('') end |
#shuffle! ⇒ Object
Destructive version of String#shuffle.
Returns: String or nil
26 27 28 29 |
# File 'lib/extra/string.rb', line 26 def shuffle! shuffled = shuffle self[0..-1] = shuffled unless shuffled == self end |
#to_proc ⇒ Object
Convenience method for things such as array mapping. This is identical to the version in Symbol.
Example: [1, 2, 3].map(&'succ') #=> [2, 3, 4]
Returns: Proc object
50 51 52 |
# File 'lib/extra/string.rb', line 50 def to_proc proc { |obj| obj.__send__(self) } end |
#to_rxp ⇒ Object
Convert a string to a Regexp object.
Example: 'foo'.to_rxp #=> /foo/
Returns: Regexp object
185 186 187 |
# File 'lib/extra/string.rb', line 185 def to_rxp Regexp.new(self) end |
#truncate(length, ending = '') ⇒ Object
Truncate a string to a certain length, and optionally append ending characters to it.
Example: 'foobarbaz'.truncate(3, '...') #=> "foo..."
Returns: String
119 120 121 122 123 124 125 |
# File 'lib/extra/string.rb', line 119 def truncate(length, ending = '') if size > length self[0...length] + ending else self end end |
#truncate!(length, ending = '') ⇒ Object
Destructive version of String#truncate.
Returns: String or nil
131 132 133 |
# File 'lib/extra/string.rb', line 131 def truncate!(length, ending = '') self[0..-1] = truncate if size > length end |
#underscore ⇒ Object
Removes everything except letters and spaces, replaces spaces with an underscore, and lowercases the string. The opposite of the camelcase convention.
Example: "foo bar baz".underscore #=> "foo_bar_baz"
Returns: String
166 167 168 |
# File 'lib/extra/string.rb', line 166 def underscore gsub(/[^a-z ]+/, '').gsub(/ +/, '_').downcase end |
#underscore! ⇒ Object
Destructive version of String#underscore.
Returns: String or nil.
174 175 176 177 |
# File 'lib/extra/string.rb', line 174 def underscore! underscored = underscore self[0..-1] = underscored unless underscored == self end |
#words ⇒ Object
Get an array of “words”.
Example: "hello, world!".words #=> ["hello", "world"]
Returns: Array
195 196 197 |
# File 'lib/extra/string.rb', line 195 def words scan(/\w+/u) end |
#wrap(width = 80, separator = $/) ⇒ Object
Wrap string by characters and join them by a specified separator.
Example: "1234".wrap(2) #=> "12\n34"
Returns: String
205 206 207 |
# File 'lib/extra/string.rb', line 205 def wrap(width = 80, separator = $/) scan(/.{1,#{width}}/u).join(separator) end |
#wrap!(width = 80, separator = $/) ⇒ Object
Destructive version of String#wrap.
Returns: String or nil
213 214 215 216 |
# File 'lib/extra/string.rb', line 213 def wrap!(width = 80, separator = $/) wrapped = wrap(width, separator) self[0..-1] = wrapped unless wrapped == self end |