class String

Public Class Methods

gimme_match_data! click to toggle source

Replaces the traditional substitution methods with their MatchData passing equivalents. USE WITH CAUTION!

# File lib/nuggets/string/sub_with_md.rb, line 81
def self.gimme_match_data!
  alias_method :sub,   :sub_with_md
  alias_method :sub!,  :sub_with_md!
  alias_method :gsub,  :gsub_with_md
  alias_method :gsub!, :gsub_with_md!

Public Instance Methods

ansicolor2css() click to toggle source
# File lib/nuggets/ansicolor2css.rb, line 118
def ansicolor2css
Also aliased as: ansicolour2css
Alias for: ansicolor2css
capitalize_first → new_string click to toggle source

Capitalizes the first character in str, but without downcasing the rest like String#capitalize does.

# File lib/nuggets/string/capitalize_first.rb, line 34
def capitalize_first
  empty? ? self : self[0..0].upcase << self[1..-1]
capitalize_first! → str click to toggle source

Destructive version of capitalize_first.

# File lib/nuggets/string/capitalize_first.rb, line 42
def capitalize_first!
capitalized? → +true+ or +false+ click to toggle source

Tell whether str is capitalized.

# File lib/nuggets/string/case.rb, line 77
def capitalized?
  self == capitalize
case → aSymbol click to toggle source

Returns a symbol indicating the case of str.

# File lib/nuggets/string/case.rb, line 41
def case
  self == downcase ? Case::LOWER :
  self == upcase   ? Case::UPPER :
Alias for: lower_case?
from_dotted_decimal → anInteger click to toggle source

Converts str from dotted-decimal notation to integer.

# File lib/nuggets/dotted_decimal.rb, line 47
def from_dotted_decimal
  split('.').map { |i| i.to_i.to_binary_s(8) }.join.to_i(2)
gsub_with_md(pattern) { |match_data| ... } → new_str click to toggle source

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, line 60
def gsub_with_md(pattern, replacement = nil, &block)
  replacement ?
    gsub_without_md(pattern, replacement) :
    (_dup = dup).gsub_with_md!(pattern, &block) || _dup
gsub_with_md!(pattern) { |match_data| ... } → str or +nil+ click to toggle source

Destructive version of gsub_with_md.

# File lib/nuggets/string/sub_with_md.rb, line 70
def gsub_with_md!(pattern, replacement = nil)
  replacement ?
    gsub_without_md!(pattern, replacement) :
    gsub_without_md!(pattern) { |match| yield $~ }
lower_case? → +true+ or +false+ click to toggle source

Tell whether str is all lower case.

# File lib/nuggets/string/case.rb, line 51
def lower_case? == Case::LOWER
Also aliased as: downcase?
map_diacritics() click to toggle source
# File lib/nuggets/i18n.rb, line 145
def map_diacritics
  (_dup = dup).map_diacritics! || _dup
map_diacritics!() click to toggle source
# File lib/nuggets/i18n.rb, line 149
def map_diacritics!
  re, block = ::Nuggets::I18n.args_for_map_diacritics
  gsub!(re, &block)
mixed_case? → +true+ or +false+ click to toggle source

Tell whether str is mixed case.

# File lib/nuggets/string/case.rb, line 69
def mixed_case? == Case::MIXED
msub(*substitutions) → new_str click to toggle source

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, line 45
def msub(*substitutions)
  (_dup = dup).msub!(*substitutions) || _dup
msub!(*substitutions) → str or +nil+ click to toggle source

Destructive version of msub.

# File lib/nuggets/string/msub.rb, line 53
def msub!(*substitutions)
  options = substitutions.last.is_a?(::Hash) ? substitutions.pop : {}
  binding = options.delete(:__binding__) || ::Kernel.binding

  keys, subs, cache = [], [], {}

  substitutions.concat(options.to_a).each { |key, value|
    key = unless key.is_a?(::Regexp)

    keys << key
    subs << [key, value]

  gsub!(::Regexp.union(*keys)) { |match|
    cache[match] ||= begin
      value = subs.find { |key, _| key =~ match }.last

      if value.respond_to?(:evaluate)
        # make match available for string evaluation
        eval("__match__ = #{match.inspect}", binding)

      elsif value.respond_to?(:call)
nsub(pattern, replacement, count) → new_str click to toggle source
nsub(pattern, count) { |match| ... } → new_str

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, line 35
def nsub(*args, &block)
  (_dup = dup).nsub!(*args, &block) || _dup
nsub!(pattern, replacement, count) → str or +nil+ click to toggle source
nsub!(pattern, count) { |match| ... } → str or +nil+

Performs the substitutions of nsub in place, returning str, or nil if no substitutions were performed.

# File lib/nuggets/string/nsub.rb, line 45
def nsub!(*args)
  pattern, i = args.first, 0

  case args.size
    when 2
      # Only +count+ given
      count = args.last

      gsub!(pattern) { |match| (i += 1) <= count ? yield(match) : match }
    when 3
      # Both +replacement+ and +count+ given;
      # ignore block (just like String#gsub does)
      replacement, count = args.values_at(1, 2)

      gsub!(pattern) { |match| (i += 1) <= count ? replacement : match }
      raise ::ArgumentError, "wrong number of arguments (#{args.size} for 2-3)"
replace_diacritics → new_str click to toggle source

Substitutes any diacritics in str with their replacements as per Nuggets::I18n::DIACRITICS.

# File lib/nuggets/i18n.rb, line 117
def replace_diacritics
  (_dup = dup).replace_diacritics! || _dup
replace_diacritics! → str or +nil+ click to toggle source

Destructive version of replace_diacritics.

# File lib/nuggets/i18n.rb, line 125
def replace_diacritics!
  diacritics = ::Nuggets::I18n::DIACRITICS

  gsub!(/#{::Regexp.union(*diacritics.keys)}/) { |m|
    s = diacritics[m]

    # Try to adjust case:
    #   'Äh' => 'AEh' => 'Aeh'
    # But:
    #   'SÖS' => 'SOES' (not 'SOeS'!)
    if s.length > 1
      t = $'[0, 1]
      s[1..-1] = s[1..-1].downcase if t == t.downcase

sub_with_md(pattern) { |match_data| ... } → new_str click to toggle source

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, line 39
def sub_with_md(pattern, replacement = nil, &block)
  replacement ?
    sub_without_md(pattern, replacement) :
    (_dup = dup).sub_with_md!(pattern, &block) || _dup
sub_with_md!(pattern) { |match_data| ... } → str or +nil+ click to toggle source

Destructive version of sub_with_md.

# File lib/nuggets/string/sub_with_md.rb, line 49
def sub_with_md!(pattern, replacement = nil)
  replacement ?
    sub_without_md!(pattern, replacement) :
    sub_without_md!(pattern) { |match| yield $~ }
Alias for: upper_case?
upper_case? → +true+ or +false+ click to toggle source

Tell whether str is all upper case.

# File lib/nuggets/string/case.rb, line 60
def upper_case? == Case::UPPER
Also aliased as: upcase?
word_wrap(line_width) → new_str click to toggle source

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, line 37
def word_wrap(line_width = 80, as_array = false)
  wrapped = []

  split(/(\n+)/).to_enum(:each_slice, 2).each { |paragraph, linebreaks|
    wrapped << paragraph.word_wrap_paragraph!(line_width) << linebreaks

  wrapped = wrapped.join

  as_array ? wrapped.split("\n") : wrapped
word_wrap!(line_width) → str click to toggle source

As with word_wrap, but modifies the string in place.

# File lib/nuggets/string/word_wrap.rb, line 53
def word_wrap!(line_width = 80)
word_wrap_paragraph(line_width) → new_str click to toggle source

Similar to word_wrap, but assumes a single paragraph.

# File lib/nuggets/string/word_wrap.rb, line 61
def word_wrap_paragraph(line_width = 80)
  (_dup = dup).word_wrap_paragraph!(line_width) || _dup
word_wrap_paragraph!(line_width) → str click to toggle source

Destructive version of word_wrap_paragraph.

# File lib/nuggets/string/word_wrap.rb, line 69
def word_wrap_paragraph!(line_width = 80)
  gsub!(/(.{1,#{line_width}})(?:\s+|$)/, "\\1\n")
  sub!(/\n$/, '')