class String
Public Class Methods
Replaces the traditional substitution methods with their MatchData passing equivalents. USE WITH CAUTION!
# File lib/nuggets/string/sub_with_md.rb 81 def self.gimme_match_data! 82 alias_method :sub, :sub_with_md 83 alias_method :sub!, :sub_with_md! 84 alias_method :gsub, :gsub_with_md 85 alias_method :gsub!, :gsub_with_md! 86 end
Public Instance Methods
# File lib/nuggets/ansicolor2css.rb 118 def ansicolor2css 119 ::Nuggets::ANSIColor2CSS.convert(self) 120 end
Capitalizes the first character in str
, but without downcasing the rest like String#capitalize does.
# File lib/nuggets/string/capitalize_first.rb 34 def capitalize_first 35 empty? ? self : self[0..0].upcase << self[1..-1] 36 end
Destructive version of capitalize_first
.
# File lib/nuggets/string/capitalize_first.rb 42 def capitalize_first! 43 replace(capitalize_first) 44 end
Tell whether str is capitalized.
# File lib/nuggets/string/case.rb 77 def capitalized? 78 self == capitalize 79 end
Returns a symbol indicating the case of str.
# File lib/nuggets/string/case.rb 41 def case 42 self == downcase ? Case::LOWER : 43 self == upcase ? Case::UPPER : 44 Case::MIXED 45 end
Converts str from dotted-decimal notation to integer.
# File lib/nuggets/dotted_decimal.rb 47 def from_dotted_decimal 48 split('.').map { |i| i.to_i.to_binary_s(8) }.join.to_i(2) 49 end
Just like gsub, but passes the MatchData object instead of the current match string to the block.
# File lib/nuggets/string/sub_with_md.rb 60 def gsub_with_md(pattern, replacement = nil, &block) 61 replacement ? 62 gsub_without_md(pattern, replacement) : 63 (_dup = dup).gsub_with_md!(pattern, &block) || _dup 64 end
Destructive version of gsub_with_md
.
# File lib/nuggets/string/sub_with_md.rb 70 def gsub_with_md!(pattern, replacement = nil) 71 replacement ? 72 gsub_without_md!(pattern, replacement) : 73 gsub_without_md!(pattern) { |match| yield $~ } 74 end
Tell whether str is all lower case.
# File lib/nuggets/string/case.rb 51 def lower_case? 52 self.case == Case::LOWER 53 end
# File lib/nuggets/i18n.rb 146 def map_diacritics 147 (_dup = dup).map_diacritics! || _dup 148 end
# File lib/nuggets/i18n.rb 150 def map_diacritics! 151 re, block = ::Nuggets::I18n.args_for_map_diacritics 152 gsub!(re, &block) 153 end
Tell whether str is mixed case.
# File lib/nuggets/string/case.rb 69 def mixed_case? 70 self.case == Case::MIXED 71 end
Performs multiple substitutions on str with order being taken into account (thus results of previous substitutions won't be subject to later ones) – inspired by Ruby Cookbook example 1.18.
The substitutions
parameter can be an array or a list of [pattern, substitution]
pairs, or, simply, a hash. Note that, when using a hash, the ordering of how substitutions are processed might differ from what you intended – instead use an array when order matters. pattern
can be a string or a regexp, substitution
can be a string (which may contain string expressions; cf. evaluate
), a proc (which will be call()ed), or any object really (which will be converted into a string).
# File lib/nuggets/string/msub.rb 45 def msub(*substitutions) 46 (_dup = dup).msub!(*substitutions) || _dup 47 end
Destructive version of msub
.
# File lib/nuggets/string/msub.rb 53 def msub!(*substitutions) 54 options = substitutions.last.is_a?(::Hash) ? substitutions.pop : {} 55 binding = options.delete(:__binding__) || ::Kernel.binding 56 57 keys, subs, cache = [], [], {} 58 59 substitutions.concat(options.to_a).each { |key, value| 60 key = ::Regexp.new(::Regexp.escape(key)) unless key.is_a?(::Regexp) 61 62 keys << key 63 subs << [key, value] 64 } 65 66 gsub!(::Regexp.union(*keys)) { |match| 67 cache[match] ||= begin 68 value = subs.find { |key, _| key =~ match }.last 69 70 if value.respond_to?(:evaluate) 71 # make match available for string evaluation 72 eval("__match__ = #{match.inspect}", binding) 73 74 value.evaluate(binding) 75 elsif value.respond_to?(:call) 76 value.call(match) 77 else 78 value 79 end 80 end 81 } 82 end
Returns a copy of str with the first count
occurrences of pattern replaced with either replacement
or the value of the block.
# File lib/nuggets/string/nsub.rb 35 def nsub(*args, &block) 36 (_dup = dup).nsub!(*args, &block) || _dup 37 end
Performs the substitutions of nsub
in place, returning str, or nil
if no substitutions were performed.
# File lib/nuggets/string/nsub.rb 45 def nsub!(*args) 46 pattern, i = args.first, 0 47 48 case args.size 49 when 2 50 # Only +count+ given 51 count = args.last 52 53 gsub!(pattern) { |match| (i += 1) <= count ? yield(match) : match } 54 when 3 55 # Both +replacement+ and +count+ given; 56 # ignore block (just like String#gsub does) 57 replacement, count = args.values_at(1, 2) 58 59 gsub!(pattern) { |match| (i += 1) <= count ? replacement : match } 60 else 61 raise ::ArgumentError, "wrong number of arguments (#{args.size} for 2-3)" 62 end 63 end
Substitutes any diacritics in str with their replacements as per Nuggets::I18n::DIACRITICS.
# File lib/nuggets/i18n.rb 118 def replace_diacritics 119 (_dup = dup).replace_diacritics! || _dup 120 end
Destructive version of replace_diacritics
.
# File lib/nuggets/i18n.rb 126 def replace_diacritics! 127 diacritics = ::Nuggets::I18n::DIACRITICS 128 129 gsub!(/#{::Regexp.union(*diacritics.keys)}/) { |m| 130 s = diacritics[m] 131 132 # Try to adjust case: 133 # 'Äh' => 'AEh' => 'Aeh' 134 # 135 # But: 136 # 'SÖS' => 'SOES' (not 'SOeS'!) 137 if s.length > 1 138 t = $'[0, 1] 139 s[1..-1] = s[1..-1].downcase if t == t.downcase 140 end 141 142 s 143 } 144 end
Just like sub, but passes the MatchData object instead of the current match string to the block.
# File lib/nuggets/string/sub_with_md.rb 39 def sub_with_md(pattern, replacement = nil, &block) 40 replacement ? 41 sub_without_md(pattern, replacement) : 42 (_dup = dup).sub_with_md!(pattern, &block) || _dup 43 end
Destructive version of sub_with_md
.
# File lib/nuggets/string/sub_with_md.rb 49 def sub_with_md!(pattern, replacement = nil) 50 replacement ? 51 sub_without_md!(pattern, replacement) : 52 sub_without_md!(pattern) { |match| yield $~ } 53 end
Tell whether str is all upper case.
# File lib/nuggets/string/case.rb 60 def upper_case? 61 self.case == Case::UPPER 62 end
Word wrap a string not exceeding line_width
. Based on the Ruby Facets implementation, but preserves paragraphs. Thus str == str.word_wrap(str.split("\n").map { |l| l.length }.max)
.
# File lib/nuggets/string/word_wrap.rb 37 def word_wrap(line_width = 80, as_array = false) 38 wrapped = [] 39 40 split(/(\n+)/).to_enum(:each_slice, 2).each { |paragraph, linebreaks| 41 wrapped << paragraph.word_wrap_paragraph!(line_width) << linebreaks 42 } 43 44 wrapped = wrapped.join 45 46 as_array ? wrapped.split("\n") : wrapped 47 end
As with word_wrap
, but modifies the string in place.
# File lib/nuggets/string/word_wrap.rb 53 def word_wrap!(line_width = 80) 54 replace(word_wrap(line_width)) 55 end
Similar to word_wrap
, but assumes a single paragraph.
# File lib/nuggets/string/word_wrap.rb 61 def word_wrap_paragraph(line_width = 80) 62 (_dup = dup).word_wrap_paragraph!(line_width) || _dup 63 end
Destructive version of word_wrap_paragraph
.
# File lib/nuggets/string/word_wrap.rb 69 def word_wrap_paragraph!(line_width = 80) 70 gsub!(/(.{1,#{line_width}})(?:\s+|$)/, "\\1\n") 71 sub!(/\n$/, '') 72 73 self 74 end