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
-
#capitalize_words(*disclude) ⇒ Object
Capitalize words.
-
#class_exists?(class_name = nil) ⇒ Boolean
Check self or provider string is defined as an constant or constantize.
-
#munge ⇒ Object
My unsubmitted answer to a previous RubyQuiz question.
-
#munge! ⇒ Object
Destructive version of String#munge.
- #random(length = 8, seed = nil) ⇒ Object
-
#shuffle ⇒ Object
Randomly shuffle a string.
-
#shuffle! ⇒ Object
Destructive version of String#shuffle.
-
#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
20 21 22 23 |
# File 'lib/extra_lib/core_ext/string.rb', line 20 def self.random(length = 8, seed = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789') length ||= 8 s = ''; length.times { s << seed.shuffle[0] }; s end |
Instance Method Details
#capitalize_words(*disclude) ⇒ Object
Capitalize words. Example: 'The dog is stupid'.capitalize_words('is') #=> "The Dog is Stupid"
Returns: String with words capitalized.
62 63 64 65 66 67 |
# File 'lib/extra_lib/core_ext/string.rb', line 62 def capitalize_words(*disclude) disclude = disclude.flatten # flatten(level) # Returns a new array that is a one-dimensional flattening of self (recursively). gsub(/\w+/u) { |word| disclude.include?(word) ? word : word.capitalize } end |
#class_exists?(class_name = nil) ⇒ Boolean
Check self or provider string is defined as an constant or constantize.
Example: "String".class_exists? #=> true
Returns: boolen
9 10 11 12 |
# File 'lib/extra_lib/core_ext/string.rb', line 9 def class_exists?(class_name = nil) class_name = self if class_name.nil? (class_name.capitalize.constantize.is_a?(Class) ? true : false rescue false) or (class_name.classify.constantize.is_a?(Class) ? true : false rescue false) 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
77 78 79 80 81 82 83 84 85 |
# File 'lib/extra_lib/core_ext/string.rb', line 77 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.
91 92 93 94 |
# File 'lib/extra_lib/core_ext/string.rb', line 91 def munge! munged = munge self[0..-1] = munged unless munged == self end |
#random(length = 8, seed = nil) ⇒ Object
25 26 27 28 |
# File 'lib/extra_lib/core_ext/string.rb', line 25 def random(length = 8, seed = nil) seed = self if seed.nil? self.class.random(length, self) end |
#shuffle ⇒ Object
Randomly shuffle a string.
Example: 'foobar'.shuffle #=> bofoar
Returns: String
36 37 38 |
# File 'lib/extra_lib/core_ext/string.rb', line 36 def shuffle split(//u).sort_by { rand }.join('') end |
#shuffle! ⇒ Object
Destructive version of String#shuffle.
Returns: String or nil
44 45 46 47 |
# File 'lib/extra_lib/core_ext/string.rb', line 44 def shuffle! shuffled = shuffle self[0..-1] = shuffled unless shuffled == self end |
#words ⇒ Object
Get an array of “words”.
Example: "hello, world!".words #=> ["hello", "world"]
Returns: Array
102 103 104 |
# File 'lib/extra_lib/core_ext/string.rb', line 102 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
112 113 114 |
# File 'lib/extra_lib/core_ext/string.rb', line 112 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
120 121 122 123 |
# File 'lib/extra_lib/core_ext/string.rb', line 120 def wrap!(width = 80, separator = $/) wrapped = wrap(width, separator) self[0..-1] = wrapped unless wrapped == self end |