class String
Public Class Methods
Generate a random string with a given length and seed.
Example: String.random(4, 'abcdefg') #=> "cdeg"
Returns: String
# 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
Public Instance Methods
Capitalize words. Example: 'The dog is stupid'.capitalize_words('is') #=> "The Dog is Stupid"
Returns: String
with words capitalized.
# 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
Check self or provider string is defined as an constant or constantize.
Example: "String".class_exists? #=> true
Returns: boolen
# 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
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
# 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
Destructive version of String#munge
.
Returns: Munged string or nil.
# File lib/extra_lib/core_ext/string.rb, line 91 def munge! munged = munge self[0..-1] = munged unless munged == self end
# 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
Randomly shuffle a string.
Example: 'foobar'.shuffle #=> bofoar
Returns: String
# File lib/extra_lib/core_ext/string.rb, line 36 def shuffle split(//u).sort_by { rand }.join('') end
Destructive version of String#shuffle
.
Returns: String
or nil
# File lib/extra_lib/core_ext/string.rb, line 44 def shuffle! shuffled = shuffle self[0..-1] = shuffled unless shuffled == self end
Get an array of “words”.
Example: "hello, world!".words #=> ["hello", "world"]
Returns: Array
# File lib/extra_lib/core_ext/string.rb, line 102 def words scan(/\w+/u) end
Wrap string by characters and join them by a specified separator.
Example: "1234".wrap(2) #=> "12\n34"
Returns: String
# File lib/extra_lib/core_ext/string.rb, line 112 def wrap(width = 80, separator = $/) scan(/.{1,#{width}}/u).join(separator) end
Destructive version of String#wrap
.
Returns: String
or nil
# 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